1

Why is it that in Java, i-- and --i have the same behavior inside a for loop ?

ex.: My variable "i" doesn't decrease before looping:

for(int i = 5; i > 0; --i) {
    System.out.println(i);
}

and

for(int i = 5; i > 0; i--) {
    System.out.println(i);
}

... will both print 5,4,3,2,1.

But this:

int i = 5;
System.out.println(--i);


int i = 5;
System.out.println(i--);

...will print 4 and 5.

mhlz
  • 3,497
  • 2
  • 23
  • 35
Begoodpy
  • 1,018
  • 3
  • 8
  • 20
  • 2
    See this question on the difference between the pre and post decrement operators: http://stackoverflow.com/questions/5413548/java-prefix-postfix-of-increment-decrement-operators – Matthew Mar 03 '15 at 14:52
  • 2
    "Why --i doesn't work in java for loop?" - It works perfectly. – zubergu Mar 03 '15 at 14:55

4 Answers4

4

It's because for loop works like this:

for (<1. variable declaration and initialization>;
     <2. condition to loop>;
     <4. for update>) {
    <3. statements>
}

Your i-- or --i condition is executed after the execution of the statements in the for loop and before checking the condition to loop. Which means, it doesn't matter if you use i-- or --i in the for update section.

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
4

Both --i and i-- have the same side effect, decrementing i by one. They differ in their result value. In the loop code you are only using the side effect, ignoring the result. In the freestanding println code you are displaying the result.

Patricia Shanahan
  • 25,849
  • 4
  • 38
  • 75
3

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);
}
mhlz
  • 3,497
  • 2
  • 23
  • 35
  • Thanks mhlz! Can we state then that "for(int i = 5; i > 0; --i)" is exactly the same as "for(int i = 5; i > 0; i--)" ? – Begoodpy Mar 03 '15 at 15:13
  • Both will produce exactly the same result, so I guess you could say that. – mhlz Mar 03 '15 at 15:16
0

I think the simplest way to put it is that, in the loops, you are printing like this:

System.out.println(i);

Notice the argument to println() is "i", not "i--" or "--i". The decrement has already taken place elsewhere. You are not printing the result of the decrement but the value of "i" within the loop.

BarrySW19
  • 3,759
  • 12
  • 26