Break statement in C#
Break statement Terminates the loop or switch statement and transfers execution to the statement
immediately following the loop or switch.
Example
using System;
namespace Break_statement
{
class Program
{
static void Main(string[] args)
{
/* local variable definition */
int x= 1;
/* while loop execution */
while (x < 10)
{
Console.WriteLine("value of x = {0}", x);
x++;
if (x >5)
{
/* terminate the loop using break statement */
break;
}
}
Console.ReadLine();
}
}
}
Output
value of x = 1
value of x = 2
value of x = 3
value of x = 4
value of x = 5
Break statement Terminates the loop or switch statement and transfers execution to the statement
immediately following the loop or switch.
Example
using System;
namespace Break_statement
{
class Program
{
static void Main(string[] args)
{
/* local variable definition */
int x= 1;
/* while loop execution */
while (x < 10)
{
Console.WriteLine("value of x = {0}", x);
x++;
if (x >5)
{
/* terminate the loop using break statement */
break;
}
}
Console.ReadLine();
}
}
}
Output
value of x = 1
value of x = 2
value of x = 3
value of x = 4
value of x = 5