1

I just want to understand the difference between for(int i =1;i<4;i++) and for(int i =1;i++<4;)
The first one prints 123
The second one prints 234

for(int i =1;i<4;i++)
    System.out.print(i);

for (int i =1;i++<4;)
    System.out.print(i);

I do not understand why are the results different, I expect 123 from both of them.

Dharman
  • 30,962
  • 25
  • 85
  • 135
omar el
  • 7
  • 3
  • 1
    detailed description(aka specification) of `for` loop: Java Language Specification [14.14.1. The basic for Statement](https://docs.oracle.com/javase/specs/jls/se13/html/jls-14.html#jls-14.14.1) – user85421 Oct 18 '19 at 16:44

3 Answers3

1

This loop:

for (int i =1;i++<4;)

increments i before System.out.print(i), which means the first printed value of i will be 2.

kmoser
  • 8,780
  • 3
  • 24
  • 40
  • does this means thaat the compiler checks it as 123 smaller than 4 but prints it as 234? – omar el Oct 18 '19 at 16:41
  • @omar el Basically that is the case indeed. – n247s Oct 18 '19 at 19:17
  • @omarel ``i++`` evaluates to the value of ``i`` *before* it is incremented. So the first time through the loop, it is evaluating whether ``1 < 4``. For more information see https://stackoverflow.com/questions/2371118/how-do-the-post-increment-i-and-pre-increment-i-operators-work-in-java – kmoser Oct 19 '19 at 02:55
0

In the standard for loop, i is incremented after the loop iteration. From the Java Tutorials:

The increment expression is invoked after each iteration through the loop; it is perfectly acceptable for this expression to increment or decrement a value.

In the second example, it is incremented in the expression where it is evaluated, but because the post increment operator is used, the value evaluated is the old value. This means that by the time it is printed, it has already been incremented.

hat
  • 781
  • 2
  • 14
  • 25
0

To explain I've refactored the statements only by using curly braces...

for(int i =1;i<4;i++) {
        System.out.print(i);
    }
System.out.println();
for (int i =1;i++<4;) {
        System.out.print(i);
    }
System.out.println();

Inside the first 'for' statement - the third clause with "i++" doesn't happen until the contents of the loop is executed.
Next consider the second 'for' statement, the comparison clause (the 2nd clause). that will be completely evaluated before the contents of the loop is started. So going into the first iteration, "i++" gets evaluated as "1" for the purpose of the comparison, but immediately gets incremented after the boolean clause is evaluated.
So when it hits the print statement it's already equal to 2. (and so on)
I hope that helps!

Pete Kelley
  • 3,713
  • 2
  • 16
  • 17
  • ohhh thank you so much so the third part of the for loop is always applied after the task is executed ?? – omar el Oct 18 '19 at 16:44
  • that is correct - more explanation re the structure : https://www.w3schools.com/java/java_for_loop.asp and refer to the link from Carlos Heuberger above. – Pete Kelley Oct 18 '19 at 16:56