class LoopTest100{ static int i,j; public static void main(String[] args) { for (i = 0;i <3;i++,System.out.print(i)) { } System.out.println(); for (j = 0;j<3;++j) { System.out.print(j); } } }
That thread dealt with "int x=1,y; y=x++ + ++x + ++x;", an interesting case but not the question at hand.
The trick here is that the first print() is in the increment part of the for statement, an unusual but legal structure. Think about the value of i after i++ during each iteration.
There is a difference indeed. The point is that the increment expression is evaluated at the end of the loop. That means that in the first loop the expression i++ will be evaluated to 1 in the first loop, then will be printed by the System.out.print(i). So the printing will start in 1 not in 0.
The second loop is different because the code block of loop is evaluated before the increment expression, so the value for j is evaluated before it is incremented. This is why this loops starts with 0.