In C#, we use the break and continue statements to control the flow of iterations in our code. Both commands help us to stop an iteration of our code. While the break
statement stops an iteration and “breaks” out from the entire parent loop in which it appears, the continue
statement stops a specific iteration but “continues” the parent loop in which it appears.
In this article, we are going to discuss just how they work, and where these statements can come in handy.
Let’s start.
A Loop Flow Without break and continue Statements
Before we begin, let’s take a look at the typical loop in programming:
Our iteration is made up of two processes. After executing both processes, we check if our loop meets its exit condition. If this is true, we exit the loop and return control to the rest of our program. Otherwise, we have to return control to the start of the loop.
Now, let’s see how the break
statement can modify this program flow.
The break Statement
We use the break
statement to halt an iteration and break out of the enclosing loop. If we place a break
statement just after Process 1 in our iteration, it will cause the program to stop the iteration without executing Process 2, and exit the loop entirely:
In this case, the control will not return to the start of our loop, and we don’t execute Process 2 or check the exit conditions.
When to Use the break Statement
We use the break
statement whenever we need to terminate an iteration and its immediate loop at any point, whether the exit condition for that loop has been met or not.
Let’s use the break
statement in a for
loop example:
for(var i = 1; i <= 10; i++) { if(i == 5) { break; } Console.WriteLine($"Our current number is: {i}"); }
In this simple for
loop that prints integers from 1 to 10, we use the break
statement to cause the execution to terminate at i == 5
. So, the iteration will repeat for i == 1, 2, 3, and 4
, but as soon as we reach i == 5
, our inner if
condition becomes true, and we hit the break statement. This causes our program to break out of the loop without reaching the next line that should print the number 5.
The continue Statement
We use the continue
statement to halt an iteration wherever it appears in our code. Rather than break out of the enclosing loop, it simply skips over any processes following it:
This means that while we won’t execute Process 2, we will still have to return to the start of our loop as long as the loop has not met its exit conditions.
When to Use the continue Statement
Whenever we need to skip out of a single iteration within a loop, we use the continue statement.
Let’s use the continue
statement in our example:
var i = 0; while(i <= 10) { i++; if(i % 3 != 0) { continue; } Console.WriteLine($"{i} is a multiple of 3"); }
In our while loop that prints out all integers from 1 to 10, we modify the execution to print only multiples of 3. To achieve this, we are printing i
only when i % 3 == 0
, otherwise, we are skipping to the next iteration.
Conclusion
The break
and continue
statements in C# are very important in the control of iterations in our programming. Being able to break out of an iteration and prevent unnecessary processing could be the difference between a great app and an inefficient one.