Mega Code Archive

 
Categories / C# Book / 01 Language Basics
 

0084 break statement

break statement is used to jump out of a loop for a switch. The following code terminates the for loop by using the break statement. using System; class Program { static void Main(string[] args) { for (int i = 0; i < 10; i++) { if (i == 5) { break; } Console.WriteLine(i); } } } The output: 0 1 2 3 4 We can also use break statement to jump out of a while loop. using System; class Program { static void Main(string[] args) { int i = 10; while (i > 0) { Console.WriteLine(i); if (i == 5) { break; } i--; } } } The output: 10 9 8 7 6 5