Skip to main content

How to Skip to the Next Loop Iteration with continue in C#

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

Sometimes a loop should ignore some items, such as blank lines in a file or messages that aren't meant for you. In C#, continue skips the rest of the loop's body for the current item and moves straight on to the next one.

Syntax​

Syntax
foreach (<Type> <item> in <collection>)
{
if (<skipCondition>)
{
continue;
}

<statements that run only for items that weren't skipped>
}
PartWhat it does
if (<skipCondition>)Decides which items to skip.
continue;Ends this pass through the loop at once; the loop goes on with the next item.
The statements after itRun only for items the condition didn't skip.

continue works the same way in for, while and do loops. Microsoft's jump statements reference: "The continue statement starts a new iteration of the closest enclosing iteration statement (that is, for, foreach, while, or do loop)".

The Example​

The example walks through the numbers 1 to 6 and prints only the odd ones, skipping each even number with continue.

Prerequisites​

  • The .NET 10 SDK or newer, which runs a single .cs file with dotnet run. This guide uses .NET SDK 10.0.404.

Step 1: Write the Code​

Create a file named main.cs in an empty folder:

main.cs, which loops over the numbers 1 to 6, calls continue when a number is even, and prints the number otherwise.

main.cs
int[] numbers = [1, 2, 3, 4, 5, 6];

foreach (int n in numbers)
{
if (n % 2 == 0)
{
continue;
}

Console.WriteLine(n);
}

n % 2 == 0 is true for even numbers, because dividing them by 2 leaves nothing over.

Step 2: Run It​

Terminal
dotnet run main.cs
Output
1
3
5

A zsh terminal where dotnet run main.cs prints 1, 3 and 5.

For 2, 4 and 6, continue jumped past Console.WriteLine, and the loop went on with the next number. The command is the same in zsh, bash and PowerShell.

continue Compared with break​

break looks similar but ends the whole loop. With break in place of continue, the same program prints only this:

Output
1

The loop stopped at 2, the first even number, and never looked at 3 to 6. The reference: "The break statement terminates the closest enclosing iteration statement (that is, for, foreach, while, or do loop) or switch statement."

Common Mistakes​

MistakeWhat happensDo this instead
Using break to skip one itemThe loop ends, and every later item is skipped tooUse continue
Putting a while loop's counter increment after continuecontinue jumps back to the condition before the increment runs, so the counter never changes and the loop never endsIncrement before continue, or use a for loop
Nesting the main work deeper and deeper inside if blocksHard-to-read codeSkip the unwanted items first with if (...) continue;

Troubleshooting​

SymptomLikely causeFix
The loop stops after the first skipped itembreak instead of continueChange it to continue
A while loop never endsA skipped incrementMove the increment above continue

FAQ​

Does continue work in nested loops?​

It affects only the innermost loop it's in. In a loop inside another loop, continue moves the inner loop to its next item; the outer loop carries on as normal.

Is continue the same as return?​

No. return leaves the whole method, so the loop ends too. continue only ends the current pass through the loop.

Conclusion​

if (<condition>) continue; skips the rest of the loop body for the items you don't want and goes on with the next one. Use break when you want to stop the loop altogether.

Additional Resources​