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#
using System;
class GFG{
static public void Main ()
{
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#
using System;
class GFG{
static public void Main ()
{
for ( int x = 0; x < 4; x++)
{
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#
using System;
class GFG{
static public void Main ()
{
int x = 1;
while ( true )
{
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#
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#
using System;
class GFG{
static public void Main ()
{
Console.Write( "Select option(1, 2, 3): " );
string str = Console.ReadLine();
int x = Int32.Parse(str);
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# 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 t
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
5 min read
Role of SemiColon in various Programming Languages
Semicolon is a punctuation mark (;) indicating a pause, typically between two main clauses, that is more pronounced than that indicated by a comma. In programming, Semicolon symbol plays a vital role. It is used to show the termination of instruction in various programming languages as well, like C,
5 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 program to use break statement #!/usr/bin/ruby -w i = 1
2 min read
MATLAB - Conditional Statements
Conditional statements are something that is very basic and important for every programmer. There will be some situations where a program or a particular block has to be executed only when a specific condition is True. These conditional statements will be very handy and fruitful in such situations.
5 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
Getting started with C
C language is a popular programming language that was developed in 1970 by Dennis Ritchie at Bell Labs. The C programming language was developed primarily to build the UNIX operating system. It is widely used because it is simple, powerful, efficient, and portable. Features of C Programming Language
5 min read
Examination Management System in C
Problem Statement: Write C program to build a Software for Examination Management System that can perform the following operations:Â Â Add/Delete the Details of the StudentsAttendance Monitoring of the studentsSet/Edit Eligibility criteria for examsCheck Eligible Students for ExamsPrint all the recor
9 min read
Loops (For and While) and Control Statements in Octave
Control statements are expressions used to control the execution and flow of the program based on the conditions provided in the statements. These structures are used to make a decision after assessing the variable. In this article, weâll discuss control statements like the if statement, for and whi
6 min read