In C#, the break statement is used to terminate a loop(for, if, while, etc.) or a switch statement on a certain condition. And after terminating the controls will pass to the statements that present after the break statement, if available. If the break statement exists in the nested loop, then it will terminate only those loops which contain the break statements.
Syntax:
break;
Flow Chart:

Now we will see the usage of break statement:
- Simple Loop
- Nested Loop
- Infinite Loop
- Switch-Case statement
1. Simple Loop:
Here we will discuss the use of break statements in simple for loop. As shown in the below example the for loop is programmed to execute from 0 to 20 but the output of this example is "0 1 2 3 4 5 6". Because here we break the loop when the value of x is equal to 7. If we do not use a break statement, then this loop prints 0....20 numbers.
C#
// C# program to illustrate the use of
// break statement in loop
using System;
class GFG{
static public void Main ()
{
// Here, the break statement
// terminates the loop when x = 7
for(int x = 0; x <= 20; x++)
{
if (x == 7)
{
break;
}
Console.WriteLine(x);
}
}
}
Output:
0
1
2
3
4
5
6
2. Nested Loop:
We can also use the break statements in the nested loops. If you use a break statement in the innermost loop, then control will come out only from the innermost loop. Let us discuss the use of break statement in the nested loops with the help of an example:
C#
// C# program to illustrate the use of
// break statement in nested loop
using System;
class GFG{
static public void Main ()
{
// Outer Loop
for(int x = 0; x < 4; x++)
{
// Inner Loop
for(int y = 1; y < 4; y++)
{
if (y > 2)
{
break;
}
Console.Write("#");
}
Console.Write("\n");
}
}
}
Output:
##
##
##
##
In the above example, the inner loop is programmed to execute for 4 iterations, but when the value of y is greater than 2 the inner loop stops executing because we use a break statement to restrict the number of iteration of the inner loop to 2. However, the outer loop remains unaffected.
3. Infinite Loops:
We can also use the break statement in the infinite loop to terminate the execution of the infinite loop. Let us discuss the use of break statement in the infinite loops with the help of an example:
C#
// C# program to illustrate
// infinite loop
using System;
class GFG{
static public void Main ()
{
int x = 1;
// Creating infinite loop
// using while loop
while (true)
{
// This statement will be printed
// infinite times
Console.WriteLine("Hey GeeksforGeeks");
x++;
}
}
}
In the above example, the loop condition based on which the loop terminates is always true. So, the loop will execute an infinite number of times, or we can say never terminate. So, here we use the break statement to terminate the loop when the value of x is equal to 7
C#
// C# program to illustrate the use of
// break statement in the infinite loop
using System;
class GFG{
static public void Main ()
{
int x = 1;
while (true)
{
if (x == 7)
break;
Console.WriteLine("Hey GeeksforGeeks");
x++;
}
}
}
Output:
Hey GeeksforGeeks
Hey GeeksforGeeks
Hey GeeksforGeeks
Hey GeeksforGeeks
Hey GeeksforGeeks
Hey GeeksforGeeks
4. Switch-case Statement:
As we know that the switch statement has a drawback, i.e. when the matching value is found it will execute all the statements until the end of the switch block. To avoid such type of problem we use break statement in every case. So that the break statement terminates the execution of the switch statement for nonmatching statements. As shown in the below example:
C#
// C# program to illustrate the use of
// break statement in switch-case statement
using System;
class GFG{
static public void Main ()
{
// Enter the value
Console.Write("Select option(1, 2, 3): ");
string str = Console.ReadLine();
int x = Int32.Parse(str);
// Using break statement in the switch-case
// statements
switch(x)
{
case 1:
Console.WriteLine("You select group A");
break;
case 2:
Console.WriteLine("You select group B");
break;
case 3:
Console.WriteLine("You select group C");
break;
default:
Console.WriteLine("Sorry, Not a valid selection!");
break;
}
}
}
Output:
Select option(1, 2, 3): 2
You select group B
Similar Reads
C# - continue Statement
In C#, the continue statement is used to skip over the execution part of the loop(do, while, for, or foreach) on a certain condition, after that, it transfers the control to the beginning of the loop. Basically, it skips its given statements and continues with the next iteration of the loop. Or in o
2 min read
C# Jump Statements (Break, Continue, Goto, Return and Throw)
In C#, Jump statements are used to transfer control from one point to another point in the program due to some specified code while executing the program. In, this article, we will learn to different jump statements available to work in C#.Types of Jump StatementsThere are mainly five keywords in th
4 min read
C# Loops
Looping in a programming language is a way to execute a statement or a set of statements multiple times depending on the result of the condition to be evaluated to execute statements. The result condition should be true to execute statements within loops.Loops are mainly divided into two categories:
4 min read
Ruby Break and Next Statement
In Ruby, we use a break statement to break the execution of the loop in the program. It is mostly used in while loop, where value is printed till the condition, is true, then break statement terminates the loop. Syntax : Break Example : Ruby # Ruby program to use break statement #!/usr/bin/ruby -w i
2 min read
Break Statement in C
The break in C is a loop control statement that breaks out of the loop when encountered. It can be used inside loops or switch statements to bring the control out of the block. The break statement can only break out of a single loop at a time.Let's take a look at an example:C#include <stdio.h>
5 min read
Jump Statements in C
In C, jump statements are used to jump from one part of the code to another altering the normal flow of the program. They are used to transfer the program control to somewhere else in the program. In this article, we will discuss the jump statements in C and how to use them. Types of Jump Statements
5 min read
AKTU 1st Year Sem 1 Solved Paper 2017-18 | COMP. SYSTEM & C PROGRAMMING | Sec A
Paper download link: Paper | Sem 1 | 2017-18 B.Tech. (SEM-I) THEORY EXAMINATION 2017-18 COMPUTER SYSTEM & PROGRAMMING IN C Time: 3hrs Total Marks: 100 Note:- There are three sections. Section A carries 20 marks, Section B carries 30 marks and Section C carries 50 marks.Attempt all questions. Mar
6 min read
Difference between break and continue statement in C
In this article, we will discuss the difference between the break and continue statements in C. They are the same type of statements which is used to alter the flow of a program still they have some difference between them. break statement: This statement terminates the smallest enclosing loop (i.e.
3 min read
Difference between exit() and break in C/C++
In this article, the topic is to understand the difference between exit() and break. exit(): When a user wants to exit a program from this function is used.It is a void return type function that calls all functions registered at the exit and terminates the program.File buffers are flushed, streams a
4 min read
break command in Linux with examples
The break command in Linux is primarily used within loops to exit or terminate the loop prematurely based on certain conditions. It offers a quick and convenient way to escape from a loop's execution, whether it's a for loop, a while loop, or a nested loop structure. It can also take one parameter i
2 min read