1. Introduction
There are plenty of times when you don't need to run through all the iterations of a loop—you just want to bail out early, like when you find the element you're looking for. Other times, you want to skip some work and jump to the next step (for example, you read some data and want to skip processing it). That's what controlling the flow inside a loop is all about.
The break and continue statements in C# are made exactly for this. Let's break down how they work and why they're super useful in real-world tasks.
2. The break Statement: "Stop the Presses!"
Theory and Syntax
The break statement is used to immediately exit a loop, no matter where you are inside its body. As soon as your program hits break, the loop stops running right away, and execution continues from the first line of code after the loop.
while (condition)
{
if (some-condition)
{
break;
}
// rest of the loop code
}
The same thing works for for and do-while loops too:
for (int i = 0; i < 10; i++)
{
if (some-condition)
{
break;
}
// rest of the loop code
}
Example 1: Checking if a Number is Prime
A number is prime if it can only be divided by 1 and itself. We need to check if a number is prime: loop through all possible divisors from 2 to n-1. If we find out that our number is divisible by a divisor, there's no point in checking further—the number isn't prime! Time to end the loop.
Example:
int number = 111;
bool found = false;
for (int i = 2; i < number; i++)
{
if (number % i == 0)
{
found = true;
Console.WriteLine("Found a divisor " + i );
break; // Stop the loop, nothing else to look for!
}
}
if (!found)
{
Console.WriteLine("No divisor found - number is prime.");
}
Example 2: Validating User Input
Let's write a little app, like an interactive form to collect some data:
while (true)
{
Console.Write("Enter your age: ");
string input = Console.ReadLine();
int age;
if (int.TryParse(input, out age) && age > 0)
{
Console.WriteLine($"Awesome! Your age: {age}");
break; // Data is valid—time to exit the loop!
}
else
{
Console.WriteLine("Error! Please enter a valid positive age.");
}
}
Why this is useful:
Sometimes you want to run a loop forever (or for a long time), until the user enters valid data. As soon as the event you want happens—use break and finally exit the loop.
3. The continue Statement: "Next, Please!"
Theory and Syntax
Unlike break, the continue statement doesn't end the whole loop, it just interrupts the current iteration: everything written after continue in the loop body does NOT run—you jump straight to the next iteration.
for (int i = 0; i < 10; i++)
{
if (condition)
{
continue;
}
// this part only runs if continue didn't trigger
}
Example 3: Skipping Even Numbers
Imagine you need to print only odd numbers from a range. Even ones should be skipped.
for (int i = 1; i <= 10; i++)
{
if (i % 2 == 0)
{
continue; // if the number is even—skip everything below!
}
Console.WriteLine("Odd number: " + i);
}
What's happening:
When i = 2, 4, 6... the loop hits continue and immediately jumps to the next iteration, skipping Console.WriteLine.
Step-by-step visualization:
| i | i % 2 == 0 | Action |
|---|---|---|
| 1 | false | print, no continue |
| 2 | true | continue, no print |
| 3 | false | print, no continue |
| 4 | true | continue, no print |
| ... | ... | ... |
The break and continue statements get used all the time. Seriously, in like every other loop. You'll love working with them.
GO TO FULL VERSION