In C#, the continue and break statements are used to control the flow of loops. Here's how they work and how you can use them within loops.
Continue Statement
The continue statement skips the current iteration of the loop and moves to the next iteration.
Example
class Program
{
static void Main(string[] args)
{
for (int count = 0; count < 10; count++)
{
if (count == 3)
{
continue;
}
Console.WriteLine("The number is " + count + "");
}
Console.ReadKey();
}
}
Output
Break Statement
The break statement terminates the loop immediately and transfers control to the statement that follows the loop.
Example
class Program
{
static void Main(string[] args)
{
for (int count = 0; count < 10; count++)
{
if (count == 3)
{
break;
}
Console.WriteLine("The number is " + count + "");
}
Console.ReadKey();
}
}
Output