Break Statement in C#

Break statement,  as the name suggests, is used to stop the code execution at a specified position in the program.

Let us check this out with the help of an example. Let us create a Console Application in Visual Studio. Write the following code in the application.

  1. using System;  
  2. usingSystem.Collections.Generic;  
  3. usingSystem.Linq;  
  4. usingSystem.Text;  
  5. usingSystem.Threading.Tasks;  
  6. namespaceBreakStatement  
  7. {  
  8.     class Program  
  9.     {  
  10.         static void Main(string[] args)  
  11.         {  
  12.             for (inti = 0; i < 6; i++)  
  13.             {  
  14.                 Console.WriteLine(i.ToString());  
  15.                 if (i == 4)  
  16.                     break;  
  17.             }  
  18.             Console.WriteLine("The execution has stopped!!");  
  19.             Console.ReadLine();  
  20.         }  
  21.     }  
  22. }  
Let us check the output of the program.



Well as we can see "5" is not included in the output because we specified the break statement in the code.

 

Ebook Download
View all
Learn
View all