Break Statement in C#

In this blog, I will explain you how to use the break statement in C#, It is a very simple question which is randomly ask in the interview.

Break statement is used to stop the execution of code from where you want. It means, if you want to stop or break the execution of program from any particular condition, than you need to use break statement.

Following is the simple example.

  1. using system.  
  2. namespace DemoTest  
  3. {  
  4.     class DemoClass  
  5.     {  
  6.         static void Main(string[] args)  
  7.         {  
  8.             for (int i = 0; i < 11; i++)  
  9.             {  
  10.                 Console.WriteLine("Number is :" + i.ToString());  
  11.                 if (i == 2)  
  12.                     break;  
  13.             }  
  14.             Console.WriteLine("Out side of loop");  
  15.             Console.ReadLine();  
  16.         }  
  17.     }  
  18. }   

Hope you enjoyed the blog. Thanks for reading.