Summary: in this tutorial, you’ll learn how to use the C# break statement to prematurely terminate a loop including while
, do while
, and for
loops.
Introduction to the C# break statement
The break
the statement allows you to terminate a loop prematurely including while
, do while
, and for
loop.
Here’s the syntax of the break
statement:
break;
Code language: C# (cs)
C# only allows you to use the break
statement inside a loop (or a switch
statement). This tutorial focuses on how to use the break
statement inside a loop.
In practice, you’ll use the break
statement to terminate a loop based on a condition prematurely. Typically, the condition is independent of the loop’s condition. So you’ll use the break
statement with an if
statment like this:
while (expression)
{
if (condition)
{
break;
}
}
Code language: C# (cs)
Note that before and/or after the if
block, you may have one or more statements. The following flowchart illustrates how the break
statement works inside a loop:
Likewise, you can use the break
statement inside a do while
loop like this:
do
{
if (condition)
{
break;
}
} while (expression);
Code language: C# (cs)
And the following flowchart shows how the break statement works inside a do while
loop:
and for
loop:
for (initializer; condition; iterator)
{
if (condition)
{
break;
}
}
Code language: C# (cs)
The following flowchart illustrates how the break statement works in the for
loop:
If you have a nested loop, the break
statement only terminates the enclosing loop, not both inner and outer loops. And you’ll see how it works in the examples below.
C# break statement examples
Let’s take some examples of using the C# break
statement.
1) Using C# break statement inside a for loop examples
The following example shows how to use the break
statement inside a for
loop:
string greeting = "Good Morning!";
for (int i = 0; i < greeting.Length; i++)
{
if (greeting[i] == ' ')
{
break;
}
Console.Write(greeting[i]);
}
Code language: C# (cs)
Output:
Good
Code language: C# (cs)
In this example, the for
loop iterates over the characters of the string "Good Morning!"
. The if
and break
statements terminate the loop if the current character is the space (' '
).
Therefore, the output only shows the characters from G
to d
in the string "Good Morning!"
.
2) Using the C# break statement to print a half pyramid
The following example illustrates how to use the break
statement inside a nested loop:
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
// exit the inner loop only
if(j > i)
{
break;
}
// print numbers each row
Console.Write($"{j} ");
}
Console.WriteLine();
}
Code language: C# (cs)
Output:
0
0 1
0 1 2
0 1 2 3
0 1 2 3 4
Code language: C# (cs)
In this example:
- The outer loop determines the pyramid levels from 0 to 5.
- The inner loop prints the numbers if they are less than the current level. Since the inner loop uses the
break
statement, it’ll end when the number is greater than the current level (j > i
).
3) Using the C# break statement inside a while loop
The following example calculates the total of input integers. It uses the break
statement to terminate the loop if you enter “Q” or “q”:
string input;
int total = 0;
Console.WriteLine("Enter integers to calculate total(q or Q to quit):");
while (true)
{
input = Console.ReadLine();
if(input == "Q" || input == "q")
{
break;
}
total += Convert.ToInt32 (input);
}
Console.WriteLine(total);
Code language: C# (cs)
How it works.
First, create a while
loop with condition that is always true
:
while (true)
{
//...
}
Code language: C# (cs)
Second, get the user input and assign it to the input
variable:
input = Console.ReadLine();
Code language: C# (cs)
Third, terminate the loop if the input string is either "Q"
or "q"
:
if (input == "Q" || input == "q")
{
break;
}
Code language: C# (cs)
Finally, add the input number to the total
variable:
total += Convert.ToInt32(input);
Code language: C# (cs)
Here’s a sample output:
Enter integers to calculate total(q or Q to quit):
1
2
3
q
The total is 6
Code language: C# (cs)
4) Using the C# break statement inside a do while loop
The following example illustrates how to use break statement inside do while
statement:
double total = 0,
average = 0;
int count = 0;
Console.WriteLine("Enter integers to calculate average (q or Q to quit):");
do
{
string input = Console.ReadLine();
if (input == "Q" || input == "q")
{
break;
}
total += Convert.ToInt32(input);
count++;
} while (true);
if (count > 0)
{
average = total / count;
}
Console.WriteLine($"The average is {average}");
Code language: C# (cs)
How it works.
First, declare variables to store the total, average, and input string:
double total = 0,
average = 0;
int count = 0;
Code language: C# (cs)
Second, create a do while
loop with the condition that is always true
:
do
{
//...
} while (true);
Code language: C# (cs)
Third, prompt to enter a number:
string input = Console.ReadLine();
Code language: C# (cs)
Fourth, exit the loop if the input is “q” or “Q”:
if (input == "Q" || input == "q")
{
break;
}
Code language: C# (cs)
Fifth, add the input number to the total and increase the count
variable:
total += Convert.ToInt32(input);
count++;
Code language: C# (cs)
Finally, calculate the average after the loop ends:
if (count > 0)
{
average = total / count;
}
Code language: C# (cs)
The following shows a test run:
Enter integers to calculate the average (q or Q to quit):
10
20
30
q
The average is 20
Code language: C# (cs)
Summary
- Use the
break
statement to prematurely terminate a loop includingwhile
,do while
, andfor
. - The
break
statement only terminates the eclosing loop when used in a nested loop.