It is sometimes useful to force a loop iteration. That is, you can continue the loop, but stop processing the rest of the code in your body for this particular iteration. This, in fact, goto just past the body of the cycle, until the end of the cycle. The continue statement performs this action. In while and do-while loops, the continue statement causes control to be passed directly to the conditional expression that controls the loop. In a for loop, control first passes to the iteration part of the for statement, and then to the conditional expression. For all three cycles, any intermediate code bypasses. Here is an example program that uses a continuation to output two numbers for each line: // The demonstration continues.
class Continue { public static void main(String args[]) { for(int i=0; i<10; i++) { System.out.print(i + " "); if (i%2 == 0) continue; System.out.println(""); } } }
This code uses the% operator to parity i. If so, the loop continues without printing a new line. Here is the result of this program:
0 1
2 3
4 5
6 7
8 9
Abhishekkumar
source share