For loops work like this:
for(<Part that will be executed before the loop>;
<Part that is the condition of the loop>;
<Part that will be executed at the end of each iteration) {
<statements>
}
Any for loop can thus be rewritten like so:
<Part that will be executed before the loop>
while(<Part that is the condition of the loop>) {
<statements>
<Part that will be executed at the end of each iteration>
}
Using your example to do that results in:
int i = 5; // Part that will be executed before the loop
while(i > 0) { // Part that is the condition of the loop
System.out.println(i); // statements
--i; // Part that will be executed at the end of each iteration
}
As you can see it doesn't matter for the output if it's --i
or i--
since the print call will always happen before the variable is decremented. To achieve your desired result you can try this:
int i = 5;
while(i > 0) {
--i;
System.out.println(i);
}