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;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;
}
}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);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;
}
}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]);
}Output:
GoodIn 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();
}Output:
0
0 1
0 1 2
0 1 2 3
0 1 2 3 4In 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
breakstatement, 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);How it works.
First, create a while loop with condition that is always true:
while (true)
{
//...
}Second, get the user input and assign it to the input variable:
input = Console.ReadLine();Third, terminate the loop if the input string is either "Q" or "q":
if (input == "Q" || input == "q")
{
break;
}Finally, add the input number to the total variable:
total += Convert.ToInt32(input);Here’s a sample output:
Enter integers to calculate total(q or Q to quit):
1
2
3
q
The total is 64) 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}");How it works.
First, declare variables to store the total, average, and input string:
double total = 0,
average = 0;
int count = 0;Second, create a do while loop with the condition that is always true:
do
{
//...
} while (true);Third, prompt to enter a number:
string input = Console.ReadLine();Fourth, exit the loop if the input is “q” or “Q”:
if (input == "Q" || input == "q")
{
break;
}Fifth, add the input number to the total and increase the count variable:
total += Convert.ToInt32(input);
count++;Finally, calculate the average after the loop ends:
if (count > 0)
{
average = total / count;
}The following shows a test run:
Enter integers to calculate the average (q or Q to quit):
10
20
30
q
The average is 20Summary #
- Use the
breakstatement to prematurely terminate a loop includingwhile,do while, andfor. - The
breakstatement only terminates the eclosing loop when used in a nested loop.
Thank you for your feedback!