Sometimes it is useful to force a cycle to be repeated early. That is, you may want to continue running the loop, but ignore the statement that repeats the remaining loop body.
The continue statement is a complement to the break statement.
In the while and do while loops, the continue statement causes the control to be transferred directly to the conditional expression of the control loop, and then continues the looping process.
In the For loop, the repeating expression of the loop is evaluated, and then the conditional expression is executed, and the loop resumes execution.
For these 3 loops, any intermediate code will be bypassed.
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 ("");
}
}
}
The program uses the% (modulo) operator to verify if the variable i is an even number, and if so, the loop resumes execution without outputting a new row. The results of the program are as follows:
0 1
2 3
4 5
6 7
8 9
Class Continuelabel {
public static void Main (String args[]) {
outer:for (int i=0; i<10; i++) {
for (int j=0; j<10; J + +) {
if (J > i) {
System.out.println ();
Continue outer; }
System.out.print ("" + (I * j));}}
System.out.println ();
}
}
In this example, the continue statement terminates the loop of Count J and continues counting I for the next iteration of the loop. The output of the program is as follows:
0
0 1
0 2 4
0 3 6 9
0 4 8) 12 16
0 5 10 15 20 25
0 6 12 18 24 30 36
0 7 14 21 28 35 42 49
0 8 16 24 32 40 48 56 64
0 9 18 27 36 45 54 63 72 81
For special cases that need to be repeated earlier, the Continue statement provides a structured approach to implementation.
Continue parsing of Java