public static void main(String s[]) { int i = 0; for(;i <= 5; i++ ) { System.out.println("i = " + i ); }
System.out.println("i after the loop = " + i ); } }
i = 0 i = 1 i = 2 i = 3 i = 4 i = 5 i after the loop = 6
i = 0 i = 1 i = 2 i = 3 i = 4 i = 5 i = 6
Compilation Errors
i = 0 i = 1 i = 2 i = 3 i = 4 i = 5
Correct Answer : A
Inside main an integer variable i is assigned to 0. In for loop, the initialization part is missing which is acceptable as i is already initialised.
Terminating Condition: i <= 5
Increment value: 1 (i.e.i++)
Total number of iterations: 6
For every iteration respective value of i is printed and then incremented. When i reaches 5 value 5 is printed and i is incremented to 6. As 6 > 5 loop is terminated. The next display statement prints the new value of i which is 6. Here i has scope out of for loop too since it is declared before the loop inside the main method. So its scope is in the whole main method.