Break and Continue in c#
In the C# programming language, break and continue are both reserved keywords. They are control flow statements for C# program loops that directly impact the default block behavior structure of loops and switch statements in the current program. The break keyword is used in If Else, For, While, and Do While loops to break a specific program condition, while the continue keyword is used to skip from that location and print the next iteration process statement.

So, let’s take a closer look at the break and continue reserved keywords in C# programming.
Break Statement in C# Programming.
The break statement in C# is used to immediately terminate or break an existing program loop or switch statement and transfer program control flow to the next statement outside the current program loop.
Example of Break Statement in For Loop.
using System;
class BreakStatement
{
static void Main()
{
for (int p = 1; p <= 9; p++)
{
if(p==7)
{
break; // here we use break statement to Exit the loop when p reached at 7
}
Console.WriteLine($” p value is – {p}”);
}
Console.WriteLine(“\n Loop terminated.”);
}
}
Example of break keyword usage in switch statement.
Using System;
classTypesOfGames
{
static void Main()
{
int games = 2;
switch (games)
{
case 1:
Console.WriteLine(“Cricket”);
break;
case 2:
Console.WriteLine(“Kabaddi”);
break;
case 3:
Console.WriteLine(“Kho-Kho”);
break;
case 4:
Console.WriteLine(“Ludo”);
break;
default:
Console.WriteLine(“Invalid choice of games”);
break;
}
}
}
Example of Break Statement in While Loop.
Using System;
class BreakInWhile
{
static void Main()
{
int p=9;
while(p<=20)
{
if(p==14)
{
break; // here break keyword exits the loop when p becomes 14
}
Console.WriteLine(p);
p++;
}
Console.WriteLine(“Loop terminated”);
}
}
Example of Break Statement in Do While Loop.
Using System;
class BreakInDowhile
{
static void Main()
{
int p = 7;
do
{
if (p == 11)
{
break; // Here the break keyword stops the loop when p is 11
}
Console.WriteLine(p);
p++;
}
while (p <= 20);
Console.WriteLine(“Loop terminated.”);
}
}
continue statement in C#.
The continue statement in a C# program uses the break keyword to skip the break condition in the current iteration process, continue running the next remaining program flow statement, and move to the next iteration step sequence of the current program loop, allowing the remaining program loop statements to continue executing.
Example of a continue statement in a for loop.
using System;
class ContinueStatement
{
static void Main()
{
for (int p = 4; p <= 11; p++)
{
if (p == 7)
{
continue; // here it skips the iteration process when p reaches 7
}
Console.WriteLine($”value of p – {p}”);
}
}
}
Main Differences between break and continue in C#
| Each Feature | Break statement | Continue statement |
| Action on loop | It Terminates the loop immediate where it uses | It Skips the current loop iteration process where it used |
| Effect on loop | Break statement immediate Exits the loop entirely from its position | Continue statement Moves to the next iteration process flow of program and skip current situation |
| Scope in loop | Break statement keyword Works in both loops & switch statement | But continue statement only Works in c# program loops |
Essential elements of the break and continue statements in C# programming.
Nested loops in a C# program exit only the innermost program loop with a break statement. If a C# programmer needs to break at multiple program levels in a program, use a labeled loop in the current program.
Nested loops with break example in C#.
using System;
class NestedLoop
{
static void Main()
{
bool terminate = false;
for (int p = 0; p <= 7 && ! terminate; p++)
{
for (int q = 0; q <= 7; q++)
{
if (p == 1 && q == 1)
{
terminated = true;
break;
}
Console.WriteLine($”value of p = {p}, value of q = {q}”);
}
}
Console.WriteLine(“\n Loop terminated.”);
}
}
