What's the output of the following code?
public class Main { public static void main(String args[]) { int i = 0; for (; i < 2; i=i+5) { if (i < 5) continue; System.out.println(i); } // ww w .j a v a2s.c om System.out.println(i); } }
a Compilation error /*from w w w. j a v a2s. c o m*/ b 0 5 c 0 5 10 d 10 e 0 1 5 f 5
f
First, the following line of code has no compilation errors:.
for (; i < 2; i=i+5) {
Using the initialization block is optional in a for loop.
In this case, using a semicolon (;) terminates it.
For the first for iteration, the variable i has a value of 0.
Because this value is less than 2, the following if construct evaluates to true and the continue statement executes:.
if (i < 5) continue;
Because the continue statement ignores all the remaining statements in a for loop iteration, the control doesn't print the value of the variable i, which leads the control to move on to the next for iteration.
In the next for iteration, the value of the variable i is 5.
The for loop condition evaluates to false and the control moves out of the for loop.
After the for loop, the code prints out the value of the variable i, which increments once using the code i=i+5.