Difference between break and continue in c#
How to use break and continue statement in C#
Table of contents
break in c-sharp
The break statement is used to terminate a loop(for, if, while, etc.) or a switch statement on a certain condition. After terminating, the controls will pass to the statements that are present after the break statement, if available.
Code Sample
In the code sample below, jumps out of the loop when i
is equal to 5
:
using System;
namespace MyApplication
{
class Program
{
static void Main(string[]args)
{
for (int i = 0; i < 10; i++)
{
if(i == 5)
{
break;
}
Console.WriteLine(i);
}
}
}
}
continue in c-sharp
The continue
statement breaks one iteration in the loop, if a specified condition occurs, and continues with the next iteration in the loop.
Code Sample
The example below skips the value of 5:
using System;
namespace MyApplication
{
class Program
{
static void Main(string[]args)
{
for (int i = 0; i < 10; i++)
{
if(i == 5)
{
continue;
}
Console.WriteLine(i);
}
}
}
}
Thanks for reading...
Happy Coding!