break and continue
Back to the runner at the stadium. The coach asked for a hundred laps, but sometimes the run has to stop early. The runner is looking for a lost key on the track: as soon as it turns up, the runner leaves the track, and the remaining laps are no longer needed. And if a lap has a puddle on it, the runner can drop that lap and start the next one at once.
A loop has two statements for these two cases:
breakends the whole loop: the runner leaves the track;continueends only the current iteration: the runner drops the lap and starts the next one.
You have already met break in switch: there it ended the whole switch. In a loop, it ends the loop itself. Let's find the first number from 50 to 100 that divides by 7. After the find, there is no need to check the rest, so the loop is cut short with a break statement:
for (int i = 50; i <= 100; i++)
{
if (i % 7 == 0)
{
Console.WriteLine(i); // 56
break;
}
}
Console.WriteLine("done"); // doneLet's take apart what happens:
- For the numbers from
50to55, the conditioni % 7 == 0is false, and the loop goes on. 56divides by7with no remainder, so the program prints56.breakends the loop, and the program printsdone.
break leaves the loop
break ends the whole loop, not only the braces of the if it sits in. The remaining iterations don't run, neither does the step i++, and the program moves on to the code after the loop. That is why done is printed right after 56.
Without break, the loop would check every number up to 100 and also print 63, 70, 77, 84, 91 and 98.
break is what you need when the answer turns up before the range ends: the first matching number, the first match, the first error.
continue skips an iteration
continue ends only the current iteration: the rest of the body is skipped, and the loop moves on to the next one. In a for loop, the step i++ runs before it. Let's add up only the odd numbers from 1 to 6:
int sum = 0;
for (int i = 1; i <= 6; i++)
{
if (i % 2 == 0)
{
continue;
}
sum += i;
}
Console.WriteLine(sum); // 9Let's go step by step:
- For an even
i, the conditioni % 2 == 0is true, andcontinueskips the linesum += i. - For an odd
i, the condition is false, andsum += iadds it to the sum. - After
continue, the loop does not stop:1,3and5reachsum += i, and the program prints9.
The same could be written without continue, with the condition i % 2 != 0 around sum += i. continue is handy when the body is long: the check sits at the top, and the rest of the code doesn't have to hide inside a nested if.
This is where the runner picture parts ways with the code. A runner who drops a lap may forget to make a mark in the notebook, but for never forgets: the step i++ runs after continue too.
A while loop has no such safety net. If continue comes before the line that changes the condition's variable, that line is skipped. The variable stops changing, and the loop never ends.
So a for with continue cannot be rewritten as a while by moving the step to the end of the body: continue jumps over that step.
Interviewers often ask you to explain the difference between these two statements. The short answer: break ends the whole loop, and continue ends only the current iteration.
Common mistake
break is placed in an if branch to leave it early:
int n = 5;
if (n > 3)
{
Console.WriteLine("big");
break; // ✗
}
Console.WriteLine("end");error CS0139: No enclosing loop out of which to break or continueThe program does not compile. The message means there is no loop to leave with break or continue. break ends only a loop or a switch; it cannot end an if branch. If part of a branch should not always run, put it in a nested if.
Key points
breakends the whole loop, and the program moves on to the code after it.continueskips the rest of the current iteration; in aforloop, the step runs before the next iteration.- In a
whileloop, change the condition's variable beforecontinue, or the loop never ends. breakandcontinuework only inside a loop, andbreakalso works in aswitch.