Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> C Language

Mastering the C# Continue Statement: How to Skip Loop Iterations Effectively

Mastering the C# Continue Statement: How to Skip Loop Iterations Effectively

This guide explains how the continue statement works in C#, with practical examples for for, while, foreach, and nested loops.

In C#, the continue keyword allows a loop to immediately skip the remainder of the current iteration and jump to the next cycle. When encountered, control moves to the loop’s test condition (or update expression in a for loop) before the next iteration begins.

The syntax is straightforward:

continue;

Before diving into continue, ensure you’re comfortable with these fundamentals:


Example 1: Using continue with a for loop

using System;

namespace ContinueLoop
{
    class Program
    {
        static void Main(string[] args)
        {
            for (int i = 1; i <= 5; ++i)
            {
                if (i == 3)
                {
                    continue;
                }
                Console.WriteLine(i);
            }
        }
    }
}

Output

1
2
4
5

Here, the loop prints numbers 1 through 5 but skips 3 because the continue statement is triggered when i == 3.

Note: continue is most effective when paired with conditional logic like if…else.


Example 2: Using continue with a while loop

using System;

namespace ContinueWhile
{
    class Program
    {
        static void Main(string[] args)
        {
            int i = 0;
            while (i < 5)
            {
                i++;

                if (i == 3)
                {
                    continue;
                }

                Console.WriteLine(i);
            }
        }
    }
}

Output

1
2
4
5

The behavior mirrors the for example: iteration 3 is skipped.


How the continue Statement Works

Mastering the C# Continue Statement: How to Skip Loop Iterations Effectively

Using continue in Nested Loops

Nested loops are common when processing multi‑dimensional data. continue can be applied to the inner loop to skip specific inner iterations.

using System;

namespace ContinueNested
{
    class Program
    {
        static void Main(string[] args)
        {
            int sum = 0;

            // outer loop
            for (int i = 1; i <= 3; i++)
            {
                // inner loop
                for (int j = 1; j <= 3; j++)
                {
                    if (j == 2)
                    {
                        continue;
                    }
                    Console.WriteLine("i = " + i + " j = " + j);
                }
            }
        }
    }
}

Output

i = 1 j = 1
i = 1 j = 3
i = 2 j = 1
i = 2 j = 3
i = 3 j = 1
i = 3 j = 3

The statement inside the inner loop skips the iteration where j == 2, preventing that specific pair from being printed.

For more on nested loops, see the dedicated C# Nested Loops article.


Using continue with a foreach Loop

The foreach construct also supports continue, allowing you to skip specific elements in a collection.

using System;

namespace ContinueForeach
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] num = { 1, 2, 3, 4, 5 };
            foreach (int number in num)
            {
                if (number == 3)
                {
                    continue;
                }
                Console.WriteLine(number);
            }
        }
    }
}

Output

1
2
4
5

When the loop encounters the value 3, the continue statement causes the remainder of that iteration to be skipped, so 3 never appears in the output.


C Language

  1. Mastering the C# Switch Statement: Syntax, Examples & Best Practices
  2. C# Break Statement: How & When to Use It
  3. Mastering the C++ break Statement
  4. Mastering C++ Continue Statement: Practical Examples & Loop Control
  5. Mastering C Conditional Statements: If, Else, and More
  6. Mastering the C Switch Statement: Syntax, Flow, and Practical Example
  7. Mastering the C Goto Statement: Syntax, Practical Use, and Best Practices
  8. Java Continue Statement – Mastering Loop Control with Examples
  9. Mastering C Basic Syntax: Tokens, Structure, and Semicolons
  10. Mastering C Loops: A Comprehensive Guide to Repetition Control