What continue does
The continue statement ends the current pass of a loop and jumps straight to the next one. Every statement after continue in the loop body is skipped for that pass only. The loop itself doesn't stop. In a foreach loop, the next element is taken. In a for loop, the update part runs (for example i++) and then the condition is checked.
Use it to filter. Instead of wrapping the real work in an if, you test for the case you want to skip, call continue, and write the main work below without extra nesting.
An example: the age check
This program lets someone into a bar only if their age is at least 21:
int[] ages = {10, 30, 21, 19, 25};
foreach (int age in ages) {
if (age < 21) {
continue;
}
Console.WriteLine("Someone of age " + age + " entered the bar.");
}
The output is:
Someone of age 30 entered the bar.
Someone of age 21 entered the bar.
Someone of age 25 entered the bar.
Here is what each pass does:
- Age 10 is less than 21, so
continueskips the print. - Age 30 isn't less than 21, so the print runs.
- Age 21 isn't less than 21 (the comparison is strict), so the print runs.
- Age 19 is skipped.
- Age 25 is printed.
The loop is written foreach (int age in ages). C# uses the keyword in there. A colon, as in Java's for (int age : ages), doesn't compile in C#.
The exercise, step by step
The lesson gives you the array heights, and the task is to print only the heights that are at least 170.
- Loop over the heights. Use
foreach (int height in heights). Each person is considered once, in order, and no index is needed. - Skip the short ones. Write an
ifthat testsheight < 170and callscontinuein its body. Those people are skipped without reaching the print. - Print the rest. Below the
if, callConsole.WriteLine(height). Only heights of at least 170 get that far.
int[] heights = {180, 139, 174, 190, 162};
foreach (int height in heights) {
if (height < 170) {
continue;
}
Console.WriteLine(height);
}
The output is:
180
174
190
The condition tests the case to skip (< 170), which is the opposite of the case to keep (>= 170). A common slip is to write the keep condition together with continue. That prints exactly the wrong people.
The same thing without continue
You can write the exercise with a plain if and no continue:
foreach (int height in heights) {
if (height >= 170) {
Console.WriteLine(height);
}
}
Both versions print the same thing. With one short check the second one is just as clear. continue pays off when the body is long or has several filters, because each filter becomes a single line at the top and the main work stays at one level of indentation.
Common mistakes and gotchas
- Continue in a while loop. In a
whileloop, the update isn't part of the loop header, so acontinuethat jumps over a line likei++can leave the counter unchanged and make the loop run forever. In aforloop the update is in the header and always runs. - Continue only affects the innermost loop. In nested loops, it skips to the next pass of the inner loop only.
- Continue is not break.
breakleaves the loop entirely, whilecontinueonly skips the rest of one pass. - Statements after continue never run. Anything below an unconditional
continueis dead code, and the compiler warns about it. It belongs after a check, as in the examples. - A
continueoutside a loop is a compile-time error. - Loop variable assignment. You can't assign to a
foreachvariable. If you want to skip an element, usecontinueand don't try to change the variable.
Key takeaways
continueskips the rest of the current pass and moves on to the next element.- Test for the case to skip, then write the main work below it.
- In a
forloop the update step still runs aftercontinue. - Use
breakto stop the loop andcontinueto skip one pass.