DEV Community

Sabin Sim
Sabin Sim

Posted on

31. C# (continue)

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();
    }
}
Enter fullscreen mode Exit fullscreen mode

Execution Flow

Example when i = 6

6 % 3 == 0 → true
continue executes
Console.WriteLine(i) is skipped
loop proceeds to i++
Enter fullscreen mode Exit fullscreen mode

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);
}
Enter fullscreen mode Exit fullscreen mode

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);
}
Enter fullscreen mode Exit fullscreen mode

Output:

0
1
2
3
4
Enter fullscreen mode Exit fullscreen mode

The loop stops completely.


continue Example

for (int i = 0; i < 10; i++)
{
    if (i == 5)
    {
        continue;
    }

    Console.WriteLine(i);
}
Enter fullscreen mode Exit fullscreen mode

Output:

0
1
2
3
4
6
7
8
9
Enter fullscreen mode Exit fullscreen mode

Only 5 is skipped.


3. Practical Scenario — Protecting User Input

Consider this code:

int number = int.Parse(input);
Enter fullscreen mode Exit fullscreen mode

If the user enters:

abc
Enter fullscreen mode Exit fullscreen mode

The program throws:

FormatException
Enter fullscreen mode Exit fullscreen mode

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();
    }
}
Enter fullscreen mode Exit fullscreen mode

Execution Flow Analysis

User enters "abc"

isNumeric = false
continue executes
int.Parse is skipped
loop starts next iteration
Enter fullscreen mode Exit fullscreen mode

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;
}
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

continue

skip current iteration
return to loop condition
start next iteration
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

This separates:

  • exception cases
  • normal execution

Final Summary

break exits the entire loop.
continue skips only the current iteration and moves to the next one.

Top comments (0)