C Sharp Break Statement

C# Break Statement

To break a loop or the current flow of the program at a specified condition or to switch statements, the C# break statement is used. If used inside an inner loop, it breaks the inner loop only.

Syntax:

jump-statement;    
break;   

Example:

using System;  
public class Example  
    {  
      public static void Main(string[] args)  
      {  
          for (char i = 'a'; i <= 'p'; i++)  
          {  
              if (i == 'f')  
              {  
                  break;  
              }  
              Console.WriteLine(i);  
          }  
      }  
   }

Output:

Explanation:

In the above example, we are displaying the working and behavior of the Break statement when used inside a loop.

C# Break Statement with Inner Loop:

When the break statement is used inside the inner loop, the C# break statement breaks the inner loop.

Example:

using System;  
public class Example  
    {  
      public static void Main(string[] args)  
      {  
          for(char i='a'; i<='d'; i++){      
                    for(int j=1; j<=4; j++){      
                        if(i=='c' && j==2){      
                            break;      
                        }      
                        Console.WriteLine(i +" "+ j);      
                    }      
          }      
      }  
   }

Output:

Explanation:

In the above example, we are displaying the working and behavior of the Break statement when used inside an inner loop.

Please follow and like us:
Content Protection by DMCA.com