C# Break and Continue
Sometimes a loop should not simply run until its condition fails. You may want to stop early because you found what you were looking for, or skip an item that does not need processing. The break and continue statements give you that control.
break: leave the loop
break immediately exits the innermost loop (or switch). Execution continues with the first statement after the loop.
using System;
int[] numbers = { 4, 9, 15, 22, 37, 41 };
foreach (int n in numbers)
{
if (n > 20)
{
Console.WriteLine(quot;First number above 20: {n}");
break;
}
Console.WriteLine(quot;Checked {n}");
}Checked 4
Checked 9
Checked 15
First number above 20: 22
Without break, the loop would have kept going through 37 and 41 even though the answer was already known.
continue: skip to the next iteration
continue jumps over the rest of the current iteration and goes straight to the next one (in a for loop, the iterator i++ still runs).
using System;
for (int i = 1; i <= 10; i++)
{
if (i % 3 == 0)
continue; // skip multiples of 3
Console.Write(i + " ");
}
Console.WriteLine();1 2 4 5 7 8 10
continue is handy for filtering out invalid data early, which keeps the main logic un-nested:
using System;
string[] lines = { "alice,34", "", "# comment", "bob,27", "carol,41" };
int total = 0;
foreach (string line in lines)
{
if (line.Length == 0) continue; // skip blank lines
if (line.StartsWith("#")) continue; // skip comments
string[] parts = line.Split(',');
int age = int.Parse(parts[1]);
total += age;
Console.WriteLine(quot;{parts[0]} is {age}");
}
Console.WriteLine(quot;Total age: {total}");alice is 34
bob is 27
carol is 41
Total age: 102
break in while loops
A while (true) loop with a break is the clearest way to write a loop whose exit test belongs in the middle:
using System;
int n = 27;
int steps = 0;
while (true)
{
if (n == 1) break;
n = n % 2 == 0 ? n / 2 : 3 * n + 1;
steps++;
}
Console.WriteLine(quot;Reached 1 after {steps} steps");Reached 1 after 111 steps
Nested loops
break and continue only affect the innermost loop. To leave several loops at once, put the loops in a method and return, or use a flag:
using System;
int[,] grid =
{
{ 1, 2, 3 },
{ 4, 0, 6 },
{ 7, 8, 9 }
};
bool found = false;
for (int r = 0; r < 3 && !found; r++)
{
for (int c = 0; c < 3; c++)
{
if (grid[r, c] == 0)
{
Console.WriteLine(quot;Zero at row {r}, column {c}");
found = true;
break; // leaves only the inner loop; the flag stops the outer one
}
}
}Zero at row 1, column 1
Summary
| Statement | Effect |
|---|---|
break |
Exit the innermost loop or switch immediately |
continue |
Skip the rest of this iteration, continue with the next |
return |
Exit the whole method (and therefore every loop in it) |
Tip: Use
breakandcontinueto make loops simpler, not cleverer. If a loop body has many of them, consider moving the logic into a helper method.
Test yourself
What does this print? for (int i = 0; i < 5; i++) { if (i == 2) break; Console.Write(i); }
- 01
- 012
- 0134
- 01234
What does this print? for (int i = 0; i < 5; i++) { if (i == 2) continue; Console.Write(i); }
- 01
- 0134
- 01234
- 234
Inside two nested loops, what does break in the inner loop do?
- Exits both loops
- Exits only the inner loop
- Exits the method
- Restarts the outer loop