Skip to content
Theory · Conditions and loops · lesson 9 of 13

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:

  • break ends the whole loop: the runner leaves the track;
  • continue ends 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:

C#
for (int i = 50; i <= 100; i++)
{
    if (i % 7 == 0)
    {
        Console.WriteLine(i); // 56
        break;
    }
}

Console.WriteLine("done"); // done

Let's take apart what happens:

  1. For the numbers from 50 to 55, the condition i % 7 == 0 is false, and the loop goes on.
  2. 56 divides by 7 with no remainder, so the program prints 56.
  3. break ends the loop, and the program prints done.

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:

C#
int sum = 0;

for (int i = 1; i <= 6; i++)
{
    if (i % 2 == 0)
    {
        continue;
    }
    sum += i;
}

Console.WriteLine(sum); // 9

Let's go step by step:

  1. For an even i, the condition i % 2 == 0 is true, and continue skips the line sum += i.
  2. For an odd i, the condition is false, and sum += i adds it to the sum.
  3. After continue, the loop does not stop: 1, 3 and 5 reach sum += i, and the program prints 9.

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:

C#
int n = 5;

if (n > 3)
{
    Console.WriteLine("big");
    break; // ✗
}

Console.WriteLine("end");
TEXT
error CS0139: No enclosing loop out of which to break or continue

The 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

  • break ends the whole loop, and the program moves on to the code after it.
  • continue skips the rest of the current iteration; in a for loop, the step runs before the next iteration.
  • In a while loop, change the condition's variable before continue, or the loop never ends.
  • break and continue work only inside a loop, and break also works in a switch.