C# – Break statement

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:



  1. Simple Loop
  2. Nested Loop
  3. Infinite Loop 
  4. 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#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}
}

chevron_right


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#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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");
    }
}
}

chevron_right


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#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate 
// infinte 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++;
    }
}
}

chevron_right


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#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate the use of
// break statement in the infinte loop
using System;
  
class GFG{
      
static public void Main ()
{
    int x = 1;
      
    while (true)
    {
        if (x == 7)
            break;
              
        Console.WriteLine("Hey GeeksforGeeks");
        x++;
    }
}
}

chevron_right


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#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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
    // statemets
     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;
    }
}
}

chevron_right


Output:

Select option(1, 2, 3): 2
You select group B



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.