0. The Essence of continue
continue immediately stops the current iteration and moves to the next one.
Important distinction:
- ❌ It does not terminate the loop
- ⭕ It only skips the current cycle
Execution jumps directly to the next iteration.
1. Simple Example — Skip Multiples of 3
Goal
Print numbers from 0 to 20, but exclude multiples of 3.
Full Runnable Code
using System;
class Program
{
static void Main()
{
for (int i = 0; i <= 20; i++)
{
if (i % 3 == 0)
{
continue;
}
Console.WriteLine(i);
}
Console.WriteLine("Loop finished.");
Console.ReadKey();
}
}
Execution Flow
Example when i = 6
6 % 3 == 0 → true
continue executes
Console.WriteLine(i) is skipped
loop proceeds to i++
So 6 is not printed.
The loop continues normally afterward.
Without continue
The same logic can be written like this:
if (i % 3 != 0)
{
Console.WriteLine(i);
}
Both work.
However, the continue version follows a specific style:
Filter out exceptional cases first,
then let the normal logic run below.
This style is often called a guard clause approach.
2. break vs continue
| Keyword | Current iteration | Entire loop |
|---|---|---|
| break | Stops | Stops |
| continue | Stops | Continues |
break Example
for (int i = 0; i < 10; i++)
{
if (i == 5)
{
break;
}
Console.WriteLine(i);
}
Output:
0
1
2
3
4
The loop stops completely.
continue Example
for (int i = 0; i < 10; i++)
{
if (i == 5)
{
continue;
}
Console.WriteLine(i);
}
Output:
0
1
2
3
4
6
7
8
9
Only 5 is skipped.
3. Practical Scenario — Protecting User Input
Consider this code:
int number = int.Parse(input);
If the user enters:
abc
The program throws:
FormatException
and crashes.
Goal
We want to:
- Ignore invalid input
- Ask again
- Prevent program crashes
Safe Example Using continue
using System;
class Program
{
static void Main()
{
string input;
int userNumber = 0;
do
{
Console.WriteLine("Enter number > 10 or type 'stop':");
input = Console.ReadLine();
if (input == "stop")
{
break;
}
bool isNumeric = true;
foreach (char c in input)
{
if (!char.IsDigit(c))
{
isNumeric = false;
break;
}
}
if (!isNumeric)
{
Console.WriteLine("Invalid input.");
continue;
}
userNumber = int.Parse(input);
} while (userNumber <= 10);
Console.WriteLine("Loop ended.");
Console.ReadKey();
}
}
Execution Flow Analysis
User enters "abc"
isNumeric = false
continue executes
int.Parse is skipped
loop starts next iteration
Result:
The program does not crash.
4. Why break Did Not Cause an Initialization Error
Key idea:
break completely exits the loop.
Example:
if (input == "stop")
{
break;
}
The code after this inside the loop is never executed.
So the compiler does not require all variables to be assigned.
But continue Is Different
continue jumps back to the loop condition.
Therefore:
All possible paths must still maintain valid variable states.
Otherwise the compiler will report errors.
Structural Difference
From a control-flow perspective:
break
exit loop
continue execution outside
continue
skip current iteration
return to loop condition
start next iteration
5. When continue Is Useful
Use continue when:
- many invalid conditions exist
- you want to filter early
- you want cleaner control flow
Typical pattern:
if (invalidCondition)
{
continue;
}
// main logic below
This separates:
- exception cases
- normal execution
Final Summary
breakexits the entire loop.
continueskips only the current iteration and moves to the next one.
Top comments (0)