C# break vs continue: When to Exit a Loop or Skip an Iteration
c# break vs continue: Learn the difference between break and continue in C# loops, with practical examples and guidance on when to use each.
c# break vs continue requires a clear understanding of the core syntax, runtime behavior, and practical implementation patterns demonstrated in the examples below.
In C#, break and continue are two loop control statements that alter the flow of iteration. The key difference is that break terminates the loop entirely, while continue skips the current iteration and moves to the next one. This article explains the behavior of both statements with practical examples and clarifies when each is the right choice.
The Core Difference Between break and continue
Both break and continue are used inside loop bodies to change the loop's execution. break stops the loop immediately and transfers control to the first statement after the loop. continue stops the current iteration, jumps to the loop's increment or condition check, and starts the next iteration if the condition still holds. The distinction is fundamental: break exits, continue skips.
Consider a simple for loop that prints numbers from 0 to 4. Adding a break when i == 2 stops the loop early:
for (int i = 0; i < 5; i++) { if (i == 2) break; Console.WriteLine(i); } // Output: 0 1
Using continue instead skips the printing of 2 but continues with 3 and 4:
for (int i = 0; i < 5; i++) { if (i == 2) continue; Console.WriteLine(i); } // Output: 0 1 3 4
How break Works in C# Loops
break is a jump statement that terminates the nearest enclosing loop or switch statement. In a loop, it stops all further iterations and exits the loop body. This is useful when you have found what you were looking for and no longer need to continue searching or processing.
A common pattern is searching for an item in a collection. Without break, the loop would scan the entire collection even after finding a match. With break, you can stop early:
int[] numbers = { 10, 20, 30, 40, 50 }; int target = 30; int index = -1; for (int i = 0; i < numbers.Length; i++) { if (numbers[i] == target) { index = i; break; } } Console.WriteLine(index); // 2
break also works in foreach and while loops. In a foreach loop, it stops iterating over the remaining elements. In a while loop, it exits the loop regardless of the condition.
How continue Works in C# Loops
continue skips the remaining statements in the current iteration and moves to the next iteration. It does not exit the loop. This is useful when you want to ignore certain values or conditions but still process the rest.
For example, processing only even numbers from a list:
List<int> values = new List<int> { 1, 2, 3, 4, 5 }; foreach (int value in values) { if (value % 2 != 0) continue; Console.WriteLine(value); // 2 4 }
In a for loop, continue jumps to the increment expression and then the condition check. In a while loop, it jumps directly to the condition check. This behavior can affect how you update loop variables, so be careful when using continue in while loops.
break and continue in Different Loop Types
The behavior of break and continue is consistent across for, foreach, while, and do-while loops, but there are subtle differences in how the loop's control flow is affected.
for loops
In a for loop, continue executes the increment expression before checking the condition. This means the loop variable is updated as expected.
for (int i = 0; i < 10; i++) { if (i % 2 == 0) continue; Console.WriteLine(i); // 1 3 5 7 9 }
foreach loops
foreach does not have an explicit increment expression. continue moves to the next element in the collection. break stops iterating over the collection entirely.
while loops
In a while loop, continue jumps directly to the condition check. If the loop variable is updated after the continue statement, it will not be updated, potentially causing an infinite loop.
int i = 0; while (i < 10) { if (i % 2 == 0) { i++; continue; } Console.WriteLine(i); i++; }
Without the i++ before continue, the loop would get stuck because i would never change. This is a common pitfall.
Nested Loops: Scope of break and continue
When loops are nested, break and continue affect only the innermost loop that contains them. They do not break out of or skip iterations of outer loops unless you use additional logic.
for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (j == 1) break; Console.WriteLine($"i={i}, j={j}"); } } // Output: i=0,j=0 i=1,j=0 i=2,j=0
Here, break exits the inner loop, but the outer loop continues. To break out of both loops, you need a flag, goto, or a method return. Similarly, continue in the inner loop skips only the current iteration of the inner loop.
Common Mistakes and Edge Cases
One common mistake is using continue inside a switch statement that is inside a loop. continue applies to the loop, not the switch. This is often intended, but it can be confusing if you expect continue to skip the rest of the switch case.
for (int i = 0; i < 5; i++) { switch (i) { case 2: continue; // skips the rest of the loop iteration default: Console.WriteLine(i); break; // breaks out of switch, not loop } } // Output: 0 1 3 4
Another edge case is using break inside a finally block. The finally block runs before control leaves the loop, but break inside finally can override a return or exception. This is rarely needed and can lead to unpredictable behavior.
Performance and Maintainability Considerations
From a performance perspective, break can reduce the number of iterations when a condition is met early. This can be significant in large collections or expensive computations. continue does not reduce the total number of iterations; it only skips work for specific cases. The performance impact of continue is negligible compared to the cost of the operations you skip.
Maintainability matters more. Overusing break and continue can make loops harder to read, especially when they are nested or contain complex conditions. In many cases, extracting the loop body into a separate method or using LINQ can make the intent clearer. For example, instead of a loop with continue, you can use Where to filter:
var evenNumbers = numbers.Where(n => n % 2 == 0);
However, LINQ may not always be appropriate if you need to break early or if the loop has side effects.
Choosing Between break and continue
Use break when you need to stop the loop entirely because the goal has been achieved or further iterations are unnecessary. Use continue when you need to skip specific items but still process the rest.
A practical rule: if you are searching for a single item, break is usually the right choice. If you are filtering or transforming a sequence, continue might be useful, but a LINQ Where or Select often expresses the same logic more declaratively.
Consider the loop's exit condition. If you want the loop to end as soon as a condition is met, break is correct. If you want to ignore certain values but keep looping, continue is correct. The decision should be based on whether the loop should terminate or just skip an iteration.