Break and Continue statements in C#
Break and Continue statements in C#
Break Statement In C#, the break statement is used to terminate the execution from the current loop. A break statement is almost always associated with an if statement. Let’s see an example –
using System;
class Program
{
static void Main(string[] args)
{
for(int i=1; i <= 10 ; i++)
{
Console.WriteLine($”i={i}”);
if(i == 5)
break;
}
}
}
Output – i=1 i=2 i=3 i=4 i=5 Continue Statement The continue statement is used when you want to skip the remaining part of the loop, return to the top of the loop and continue a new iteration. The continue statement is commonly associated with an if statement. Let’s
see an example –
using System;
class Program
{
static void Main(string[] args)
{
for(int i=1; i <= 10 ; i++)
{
if(i == 5)
continue;
Console.WriteLine($”i={i}”);
}
}
}
Output – i=1 i=2 i=3 i=4 i=6 i=7 i=8 i=9 i=10
