Break
using System;
/* Break statement exits from loops, regardless of value of the test boolean expression.
* Also uses in switch blocks to exit from each case statements */
public class BreakDemo
{
static void Main(String[] args)
{
for (int i = 0; i < 5; i++)
{
if (i == 3)
{
Console.WriteLine("loop breaks when i=3");
break; //Exits from for loop
}
else
{
Console.WriteLine("Value = " + i);
}
}
Console.WriteLine("--------------------------------");
int a = 10;
switch (a)
{
case 5:
Console.WriteLine("Value of a is 5");
break; //Also used to exit from switch case
case 10:
Console.WriteLine("Value of a is 10");
break;
case 15:
Console.WriteLine("Value of a is 15");
break;
}
Console.ReadLine();
}
}
Output:
Value = 0
Value = 1
Value = 2
loop breaks when i=3
--------------------------------
Value of a is 10