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# - 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 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
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
Solidity - Break and Continue Statements
In any programming language, Control statements are used to change the execution of the program. Solidity allows us to handle loops and switch statements. These statements are usually used when we have to terminate the loop without reaching the bottom or we have to skip some part of the block of cod
2 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
Exit codes in C/C++ with Examples
The purpose of the exit() function is to terminate the execution of a program. The "return 0"(or EXIT_SUCCESS) implies that the code has executed successfully without any error. Exit codes other than "0"(or EXIT_FAILURE) indicate the presence of an error in the code. Among all the exit codes, the co
3 min read
C Program to Make a Simple Calculator
A simple calculator is a program that can perform addition, subtraction, multiplication, and division of two numbers provided as input. In this article, we will learn to create a simple calculator program in C. Example Input: a = 10, b = 5, op = +Output: 15.00Explanation: Chosen operation is additio
3 min read