The continue statement in C# is used to skip the remaining statements in the current loop iteration and move directly to the next iteration. It does not stop the whole loop. It only skips the current pass. This makes continue useful when some items should be ignored while the rest of the loop should keep running.
In real programs, continue is commonly used for filtering invalid data, skipping optional work, ignoring empty input, avoiding deeply nested if blocks, and keeping loop logic focused on the useful cases.
What Is Continue Statement in C#?
The continue statement changes the flow of a loop. When C# reaches continue, it skips the rest of the current iteration and starts the next one. It can be used inside for, while, do while, and foreach loops.
for (int i = 1; i <= 5; i++)
{
if (i == 3)
{
continue;
}
Console.WriteLine(i);
}
This prints 1, 2, 4, and 5. When i becomes 3, continue skips the print statement for that iteration.
Syntax of continue Statement
continue;
The syntax is only one keyword followed by a semicolon. Since continue does not include a condition directly, it is usually placed inside an if statement.
continue in for loop
In a for loop, continue skips the remaining statements in the loop body and then runs the iteration expression, such as i++.
for (int i = 1; i <= 10; i++)
{
if (i % 2 == 0)
{
continue;
}
Console.WriteLine(i);
}
This prints only odd numbers. Even numbers are skipped because the continue statement jumps to the next iteration before Console.WriteLine runs.
continue in while loop
In a while loop, continue jumps back to the condition check. This can be risky if the loop update happens after the continue statement, because the update may be skipped.
int i = 0;
while (i < 5)
{
i++;
if (i == 3)
{
continue;
}
Console.WriteLine(i);
}
Here, the update i++ happens before continue, so the loop remains safe. If i++ were placed after the continue, the loop could get stuck when i is 3.
continue in foreach loop
In a foreach loop, continue skips the current item and moves to the next item in the collection. This is useful when some elements should be ignored.
List<string> names = new() { "Asha", "", "Ravi", "Meera" };
foreach (string name in names)
{
if (string.IsNullOrWhiteSpace(name))
{
continue;
}
Console.WriteLine(name);
}
The empty string is skipped. The loop continues with the next valid name.
continue vs break
| Statement | Meaning | Effect |
|---|---|---|
continue | Skip current iteration | Loop keeps running |
break | Stop the loop | Loop ends immediately |
Use continue when only the current item should be skipped. Use break when no more iterations are needed.
continue in Nested Loops
In nested loops, continue affects only the nearest loop. It does not skip the outer loop unless it is used directly inside the outer loop body.
for (int row = 1; row <= 2; row++)
{
for (int col = 1; col <= 3; col++)
{
if (col == 2)
{
continue;
}
Console.WriteLine($"{row}, {col}");
}
}
This skips column 2 for each row, but the outer row loop continues normally.
Using continue for Data Filtering
A practical use of continue is filtering data inside a loop. If an item is invalid, incomplete, disabled, or not relevant, the loop can skip it early and keep the main processing logic clean.
foreach (User user in users)
{
if (!user.IsActive)
{
continue;
}
SendNotification(user);
}
This is easier to read than wrapping the whole processing block inside if (user.IsActive). The loop quickly rejects inactive users and then focuses on the valid path.
Guard-Style Loops with continue
continue can make loops flatter by acting like a guard clause. A guard clause checks a condition early and exits that path. Inside a loop, continue exits only the current iteration, which is useful when several invalid cases should be ignored before the main work starts.
foreach (Order order in orders)
{
if (order == null)
{
continue;
}
if (!order.IsPaid)
{
continue;
}
if (order.IsCancelled)
{
continue;
}
ShipOrder(order);
}
The main action ShipOrder(order) stays at the normal indentation level. Without continue, this code may become deeply nested and harder to maintain.
continue with Validation
When processing external data, not every row or item will be valid. continue lets the program skip bad records and continue processing the remaining data. This is useful in CSV imports, API payloads, log processing, and batch jobs.
foreach (string line in lines)
{
if (string.IsNullOrWhiteSpace(line))
{
continue;
}
string[] parts = line.Split(',');
if (parts.Length < 3)
{
continue;
}
ProcessRow(parts);
}
This keeps invalid rows from reaching ProcessRow. In production code, skipped records may also be logged so data problems can be reviewed later.
continue in do while loop
continue can also be used in a do while loop. When reached, it jumps to the condition check at the bottom of the loop. This means any important state update should happen before continue.
int number = 0;
do
{
number++;
if (number % 2 == 0)
{
continue;
}
Console.WriteLine(number);
}
while (number < 10);
This prints odd numbers. Since number++ happens before continue, the loop still moves forward safely.
continue vs if Wrapping
Many loops can be written either with continue or with an if wrapper. The better version is the one that is easier to read. If there is only one small condition, an if block may be fine. If there are several invalid cases, continue can reduce nesting.
| Situation | Usually Better |
|---|---|
| One small condition | if block |
| Several skip conditions | continue |
| Main work should stay flat | continue |
| Skip condition is complex | Named method or variable |
The goal is not to force continue everywhere. The goal is to make the loop intention clear.
LINQ Alternatives to continue
Sometimes a loop with continue can be replaced by a LINQ filter. If the only purpose of continue is to skip unwanted items, Where can express the filter before the loop starts.
foreach (User user in users.Where(user => user.IsActive))
{
SendNotification(user);
}
This is compact and readable for simple filters. For multi-step validation, logging, or side effects, a normal loop with continue may be clearer.
Debugging continue Problems
When a loop appears to skip too much work, check every continue condition. One condition may be true more often than expected. Logging the values before the continue can reveal why certain items never reach the main logic.
When a while loop becomes infinite, check whether continue is jumping over the update statement. In counter-based while loops, updating the counter before possible continue statements prevents many bugs.
continue Cannot Be Used Everywhere
continue can only be used inside loops. It cannot be used inside a standalone if statement, method body, or switch statement unless that switch is inside a loop and the continue applies to the loop. If you need to exit a method, use return. If you need to exit a loop completely, use break.
continue with Expensive Operations
continue can protect expensive operations from running unnecessarily. If an item fails a cheap validation check, the loop can skip it before doing database work, file processing, network calls, or heavy calculations. This keeps the expensive part focused only on items that actually qualify.
foreach (Order order in orders)
{
if (!order.IsReadyToSync)
{
continue;
}
SyncOrderWithServer(order);
}
The cheap check runs first. Only ready orders reach the sync operation. This makes the loop faster and also makes the business rule visible.
continue and Readability Tradeoff
continue improves readability when it removes unnecessary nesting. It hurts readability when it is scattered through a long loop without clear structure. If a loop has many continue statements, each with complex conditions, the reader must mentally track many possible skip paths.
A good style is to group skip conditions near the top of the loop. After the skip checks, the rest of the loop should represent the normal processing path. This creates a clean shape: reject invalid cases first, then process valid cases.
continue in Switch Inside a Loop
If a switch statement is inside a loop, continue continues the loop, not the switch. This is different from break, which exits the switch case. Because this can surprise beginners, use it only when the intention is obvious.
foreach (string command in commands)
{
switch (command)
{
case "skip":
continue;
case "run":
Console.WriteLine("Running command");
break;
}
Console.WriteLine("Command handled");
}
When the command is skip, the loop immediately moves to the next command. The line after the switch does not run for that item.
For production code, a continue statement should answer a clear question: why should this item not be processed? If that reason is obvious from the condition or a well-named method, continue usually improves the loop. If the reason is hidden inside complicated logic, the loop should be simplified before adding more flow-control statements.
That keeps the skip path intentional, safe, and easy to review.
Common Mistakes with continue Statement
- Using
continuewherebreakis needed. - Skipping the update step in a
whileloop and causing an infinite loop. - Using too many
continuestatements and making loop flow difficult to follow. - Assuming
continueaffects all nested loops. - Using
continueinstead of writing a clearer condition or filter.
Best Practices for continue Statement in C#
Use continue when skipping the current iteration makes the loop simpler. Keep the skip condition easy to see, update loop state before continuing in while loops, and avoid hiding important behavior deep inside long loop bodies.
Continue learning C# in order
Follow the topic sequence with the previous and next lesson.