The break statement stops a loop or switch immediately. It helps you exit early when you find what you need or want to stop repeating.
When searching a list and you find the item you want, stop checking the rest.
When reading user input in a loop and the user types 'exit', stop the loop.
When processing options in a switch and you want to stop after one case runs.
When you want to avoid unnecessary work after a condition is met inside a loop.
Syntax
C Sharp (C#)
break;
The break statement must be inside a loop (for, while, do-while) or a switch block.
It immediately exits the nearest enclosing loop or switch.
Examples
This loop prints numbers 0 to 4. When i is 5, break stops the loop.
C Sharp (C#)
for (int i = 0; i < 10; i++)
{
if (i == 5)
break;
Console.WriteLine(i);
}
Each case ends with break to stop checking other cases.
C Sharp (C#)
switch (day)
{
case"Monday":
Console.WriteLine("Start of week");
break;
case"Friday":
Console.WriteLine("End of week");
break;
default:
Console.WriteLine("Midweek");
break;
}
Sample Program
This program shows how break stops a loop early and ends a switch case.
C Sharp (C#)
using System;
class Program
{
staticvoid Main()
{
Console.WriteLine("Numbers until 5:");
for (int i = 0; i < 10; i++)
{
if (i == 5)
break;
Console.WriteLine(i);
}
Console.WriteLine("\nSwitch example:");
string day = "Friday";
switch (day)
{
case"Monday":
Console.WriteLine("Start of the week");
break;
case"Friday":
Console.WriteLine("End of the week");
break;
default:
Console.WriteLine("Midweek day");
break;
}
}
}
OutputSuccess
Important Notes
Without break in a switch, execution continues to the next case (called fall-through), which is usually not wanted.
Using break helps avoid unnecessary work and can improve program speed.
Summary
break stops the nearest loop or switch immediately.
Use it to exit early when a condition is met.
Always place break; inside loops or switch cases.
Practice
(1/5)
1. What does the break statement do inside a loop in C#?
easy
A. It skips the current iteration and continues the loop.
B. It immediately stops the loop and exits it.
C. It pauses the loop until a condition is true.
D. It restarts the loop from the beginning.
Solution
Step 1: Understand the role of break in loops
The break statement is designed to stop the nearest enclosing loop immediately when executed.
Step 2: Compare with other loop control statements
Unlike continue which skips to the next iteration, break exits the loop entirely.
Final Answer:
It immediately stops the loop and exits it. -> Option B