when used in a loop.
In the question j is pre incremented in the 'for' statement but in the answer j is not incremented until 1. the statement is evaluated and 2. the loop is executed.
So am I correct in saying there is no difference between a pre and post increment in a for loop statement.
public class TestClass{
public static void main(String args[]){
int i;
int j;
for (i = 0, j = 0 ; j < 1 ; ++j , i++){
System.out.println( i + " " + j );
}
System.out.println( i + " " + j );
}
}
Answer: 0 0 will be printed followed by 1 1.
j will be less than 1 for only the first iteration. So, first it will print 0, 0. Next, i and j are incremented. Because j is not less than 1 at the start of the loop, the condition fails and it comes out of the loop. Finally, it will print 1,1.
This should clear it up in my head
