C# break statement
The C# break statement immediately terminates the nearest enclosing loop or switch statement. Program execution then continues with the first statement after that loop or switch.
You can use break inside for, while, do-while, and foreach loops. It is commonly placed inside an if statement so that iteration stops when a specific condition is met.
C# break statement syntax
The syntax of break statement is:
break;
No condition is written as part of the break statement itself. The condition that decides whether to stop is normally written in a surrounding if statement.
if (condition)
{
break;
}
Example 1 – C# break statement with for loop
In the following example, we use break statement to come out of the for loop even before the condition in for loop is evaluated to false.
Program.cs
using System;
namespace CSharpExamples {
class Program {
static void Main(string[] args) {
for(int i=0;i<10;i++){
if(i==5){
break;
}
Console.WriteLine(i);
}
}
}
}
When i equals 5, the break statement terminates the for loop. Therefore, the values from 5 through 9 are not printed. Execution proceeds with any statements that follow the loop.
Output
PS D:\workspace\csharp\HelloWorld> dotnet run
0
1
2
3
4
Example 2 – C# break statement with while loop
In the following example, the while condition allows the loop to continue while i is less than 10. However, the break statement stops the loop when i becomes 5.
Program.cs
using System;
namespace CSharpExamples {
class Program {
static void Main(string[] args) {
int i=0;
while(i<10){
if(i==5){
break;
}
Console.WriteLine(i);
i++;
}
}
}
}
When i equals 5, the loop terminates immediately. The program does not execute the remaining iterations even though i < 10 is still true.
Output
PS D:\workspace\csharp\HelloWorld> dotnet run
0
1
2
3
4
Example 3 – C# break statement with foreach
In the following example, we use break statement to come out of the foreach iteration before foreach executes for all the elements in the given array.
Program.cs
using System;
namespace CSharpExamples {
class Program {
static void Main(string[] args) {
int[] nums = {2, 41, 15, 64};
foreach(int num in nums){
if(num==15){
break;
}
Console.WriteLine(num);
}
}
}
}
When num is equal to 15, the foreach loop terminates. The value 15 is not printed because the break statement is executed before Console.WriteLine(num). The remaining value, 64, is not processed.
Output
PS D:\workspace\csharp\HelloWorld> dotnet run
2
41
Using C# break in a do-while loop
A do-while loop executes its body at least once. The following program stops the loop when the counter reaches 3.
using System;
class Program
{
static void Main()
{
int number = 1;
do
{
Console.WriteLine(number);
if (number == 3)
{
break;
}
number++;
}
while (number <= 5);
Console.WriteLine("Loop ended");
}
}
Output
1
2
3
Loop ended
Using C# break in a switch statement
Inside a switch statement, break ends the selected case section and continues execution after the switch. It does not terminate the containing method.
using System;
class Program
{
static void Main()
{
int option = 2;
switch (option)
{
case 1:
Console.WriteLine("Add");
break;
case 2:
Console.WriteLine("Edit");
break;
default:
Console.WriteLine("Unknown option");
break;
}
Console.WriteLine("Switch completed");
}
}
Output
Edit
Switch completed
How C# break behaves in nested loops
In nested loops, break terminates only the nearest enclosing loop. The outer loop continues unless it is also terminated separately.
using System;
class Program
{
static void Main()
{
for (int row = 1; row <= 3; row++)
{
for (int column = 1; column <= 3; column++)
{
if (column == 2)
{
break;
}
Console.WriteLine($"Row {row}, Column {column}");
}
}
}
}
The inner loop stops when column becomes 2. The outer loop then starts its next iteration, so one line is printed for each row.
Row 1, Column 1
Row 2, Column 1
Row 3, Column 1
C# break versus continue
The break and continue statements both alter normal loop execution, but they serve different purposes:
breakterminates the entire nearest loop.continueskips the remaining statements in the current iteration and moves to the next iteration.
Use break when no more iterations are needed, such as after finding a required item. Use continue when only the current item should be skipped.
Practical rules for the C# break statement
- You can place multiple
breakstatements inside a loop for different exit conditions. - A
breakstatement affects only the nearest enclosing loop orswitch. - Statements in the same block after an unconditional
breakare unreachable and cause a compiler error. - Use
return, rather thanbreak, when you need to exit the entire method. - Place the exit condition clearly so readers can understand why the loop may terminate early.
Frequently asked questions about C# break
Can C# break exit two nested loops at once?
No. A break statement exits only the nearest enclosing loop. To stop multiple nested loops, you can use a Boolean flag, move the loops into a method and use return, or restructure the logic.
Can break be used outside a loop or switch in C#?
No. The C# compiler permits break only inside an enclosing loop or switch statement.
Does break end the C# program?
No. It ends only the nearest loop or switch. Execution continues with the next statement after that construct.
What happens when break runs before a print statement?
The loop terminates immediately, so statements later in that iteration are not executed. This is why the matching value is not printed in several examples above.
C# break statement recap
In this C# Tutorial, we learned how to use the C# break statement to terminate for, while, do-while, and foreach loops before their normal completion. We also examined its use in switch statements, its behavior in nested loops, and the difference between break and continue.
TutorialKart.com