Break and Continue Statement in C#

The break statement

The break statement is used to break out of an iteration statement block:

int MyVariable = 0;

while(MyVariable < 10)

{

System.Console.WriteLine(MyVariable);

if(MyVariable == 5)

break;

MyVariable++;

}

 System.Console.WriteLine("Out of the loop.");

The preceding code prints the following to the console:
0
1
2
3
4

Out of the loop.

The continue statement

The continue statement returns control to the Boolean expression that controls an iteration statement.
 

int MyVariable;

for(MyVariable = 0; MyVariable < 10; MyVariable++)

{

if(MyVariable == 5)

continue;

System.Console.WriteLine(MyVariable);

}

The preceding code prints the following to the console:
0
1
2
3
4
6
7
8
9

Main difference is

  • Break statement is break the iteration statement
  • The continue statement returns control to the Boolean expression that controls an iteration stateme