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.
- using System;
- usingSystem.Collections.Generic;
- usingSystem.Linq;
- usingSystem.Text;
- usingSystem.Threading.Tasks;
- namespaceBreakStatement
- {
- class Program
- {
- static void Main(string[] args)
- {
- for (inti = 0; i < 6; i++)
- {
- Console.WriteLine(i.ToString());
- if (i == 4)
- break;
- }
- Console.WriteLine("The execution has stopped!!");
- Console.ReadLine();
- }
- }
- }
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.