C# break Statement
In C#, the break statement has the following two uses:
-
When the break statement is encountered inside a loop, the loop is immediately terminated, and the program flow continues with the next statement following the loop.
-
It can be used to terminate a case in a switch statement.
If you are using nested loops (i.e., one loop inside another loop), the break statement will stop the execution of the innermost loop and start executing the next line of code after the block.
Syntax
The syntax for the break statement in C#:
break;
Flowchart
Example
Example
using System;
namespace Loops
{
class Program
{
static void Main(string[] args)
{
/* Local variable definition */
int a = 10;
/* while loop execution */
while (a < 20)
{
Console.WriteLine("Value of a: {0}", a);
a++;
if (a > 15)
{
/* Terminate the loop using break statement */
break;
}
}
Console.ReadLine();
}
}
}
When the above code is compiled and executed, it produces the following result:
Value of a: 10
Value of a: 11
Value of a: 12
Value of a: 13
Value of a: 14
Value of a: 15