continue statements can be used to restart while, do...while, for, and label statements.
Forwhile and for, continue terminates the current loop and continues execution of the loop with the next iteration
For while, continue returns to the condition
For for, continue moves to the increment expression
For label, continue restarts the statement or continues execution of a labeled loop with the next iteration. The continue statement must be in a looping statement that is identified by the label that continue uses.
Note: | In contrast to the break statement, continue does not entirely terminate the execution of the loop. |
continue statement syntax: continue or continue [label]
Example—A while loop with a continue statement that executes when the value of i is 3. Thus, n obtains the values 1, 3, 7, and 12.
Example—A statement labeled checkiandj that contains a statement labeled checkj:
checkiandj : while (i<4) { Console.Writeln(i + ""); i+=1; checkj : while (j>4) { Console.Writeln(j + ""); j-=1; if ((j%2)==0); continue checkj; Console.Writeln(j + " is odd."); } Console.Writeln("i = " + i + ""); Console.Writeln("j = " + j + ""); }
If continue is encountered, the current iteration of checkj terminates, and the next iteration of checkj begins. Iterations continue until the checkj condition returns false. When false is returned, the remainder of checkiandj is complete and checkiandj reiterates until its condition returns false. When false is returned, the statement following checkiandj executes. If continue had a label of checkiandj, checkiandj would re-execute.