C# continue statement
The C# continue statement skips the remaining statements in the current loop iteration and starts the next iteration. It can be used inside for, while, do-while, and foreach loops.
Unlike break, which exits the loop completely, continue keeps the loop running. Only the current iteration is skipped.
C# continue syntax
The syntax of continue statement is:
continue;
The statement is normally placed inside a conditional block so that only selected iterations are skipped.
if (condition)
{
continue;
}
How C# continue changes loop execution
- The loop begins an iteration.
- Statements execute until the program reaches
continue. - Any statements after
continuein that iteration are skipped. - The loop proceeds to its next iteration, provided its continuation condition still permits it.
The exact next step depends on the loop type. A for loop executes its iterator expression before checking the condition again. A while or do-while loop moves to its condition check, so required counter updates must be handled explicitly.
C# continue statement with a for loop
In the following example, we use continue statement to skip the execution of statement(s) inside for loop after continue statement for this iteration. The loop continues normally with the updated value of i until the condition of for loop statement evaluates to false.
Program.cs
using System;
namespace CSharpExamples {
class Program {
static void Main(string[] args) {
for(int i=0;i<10;i++){
if(i==5){
continue;
}
Console.WriteLine(i);
}
}
}
}
When i equals 5, the continue statement skips Console.WriteLine(i). The for loop then runs its iterator expression, increments i, and starts the next iteration.
Output
PS D:\workspace\csharp\HelloWorld> dotnet run
0
1
2
3
4
6
7
8
9
C# continue statement with a while loop
In the following example, the continue statement skips the remaining statements in the while loop when i is equal to 5.
Program.cs
using System;
namespace CSharpExamples {
class Program {
static void Main(string[] args) {
int i=0;
while(i<10){
if(i==5){
i++;
continue;
}
Console.WriteLine(i);
i++;
}
}
}
}
When i equals 5, the program increments i before executing continue. It then skips Console.WriteLine(i) and the final increment statement for that iteration.
The increment inside the if block is necessary. Without it, i would remain equal to 5, the same branch would run repeatedly, and the loop would never end.
Output
PS D:\workspace\csharp\HelloWorld> dotnet run
0
1
2
3
4
6
7
8
9
C# continue statement with foreach
In a foreach loop, continue skips the current collection element and proceeds to the next element.
Program.cs
using System;
namespace CSharpExamples {
class Program {
static void Main(string[] args) {
int[] nums = {2, 41, 15, 64};
foreach(int num in nums){
if(num==15){
continue;
}
Console.WriteLine(num);
}
}
}
}
When num is equal to 15, the program does not execute the statement after continue for that iteration. The foreach loop then processes the remaining values normally.
Output
PS D:\workspace\csharp\HelloWorld> dotnet run
2
41
64
C# continue statement with a do-while loop
A continue statement in a do-while loop transfers control to the loop condition. The loop body runs at least once because the condition is evaluated after the body.
using System;
class Program
{
static void Main()
{
int number = 0;
do
{
number++;
if (number % 2 != 0)
{
continue;
}
Console.WriteLine(number);
}
while (number < 6);
}
}
The condition identifies odd numbers. When the number is odd, continue skips the output statement. Therefore, only even numbers are printed.
2
4
6
Using C# continue to filter loop values
A common use of continue is to reject values that should not be processed. This can reduce nesting by handling excluded values first.
using System;
class Program
{
static void Main()
{
int[] values = { -4, 0, 7, 12, -1, 18 };
foreach (int value in values)
{
if (value <= 0)
{
continue;
}
Console.WriteLine($"Processing {value}");
}
}
}
Negative values and zero are skipped. Only positive values reach the processing statement.
Processing 7
Processing 12
Processing 18
C# continue behavior in nested loops
Inside nested loops, continue affects only the nearest enclosing loop. It does not skip an iteration of every surrounding loop.
using System;
class Program
{
static void Main()
{
for (int row = 1; row <= 2; row++)
{
for (int column = 1; column <= 3; column++)
{
if (column == 2)
{
continue;
}
Console.WriteLine($"Row {row}, Column {column}");
}
}
}
}
Here, continue belongs to the inner for loop. It skips column 2 for each row, while the outer loop continues normally.
Row 1, Column 1
Row 1, Column 3
Row 2, Column 1
Row 2, Column 3
C# continue versus break
| Statement | Effect on the loop | Typical use |
|---|---|---|
continue | Skips the rest of the current iteration and proceeds with the loop. | Ignore selected values while processing the remaining values. |
break | Terminates the nearest enclosing loop immediately. | Stop searching or processing after a required condition is met. |
Use continue when later iterations are still required. Use break when no further iteration should run.
Common C# continue mistakes
- Forgetting a counter update in a while loop: If
continueskips the update statement, the loop may become infinite. - Expecting continue to exit the loop: Use
breakwhen the entire loop must stop. - Placing important work after continue: Statements after an executed
continuedo not run in that iteration. - Misreading nested-loop behavior: A
continuestatement applies only to its nearest enclosing loop. - Using too many continue branches: Several scattered branches can make loop flow difficult to follow. Consider extracting validation or filtering logic into a method.
C# continue statement FAQs
Does continue stop a loop in C#?
No. continue skips only the remainder of the current iteration. The loop continues with its next iteration when its condition allows it.
Can continue be used outside a loop?
No. The continue statement must be inside a loop. Using it outside a for, foreach, while, or do-while loop causes a compile-time error.
What happens after continue in a C# for loop?
The remaining statements in the loop body are skipped. The iterator expression then runs, and the loop condition is evaluated again.
Why can continue cause an infinite while loop?
If continue executes before the variable controlling the loop is updated, that variable may never change. The same condition can then remain true indefinitely.
C# continue tutorial summary
In this C# Tutorial, we learned how the C# continue statement skips the remaining code in the current iteration without terminating the loop. We also used it with for, while, do-while, and foreach loops, and examined its behavior in nested loops.
TutorialKart.com