DEV Community

Sabin Sim
Sabin Sim

Posted on

24. C# (continue)

0. The Real Goal of This Lesson

continue means:
Terminate the current iteration immediately and move to the next one.

Important:

  • ❌ It does NOT stop the entire loop
  • ⭕ It only skips the current cycle

If you misunderstand this:

  • Loop behavior becomes confusing
  • Flow control becomes unpredictable
  • Validation logic becomes messy

This is not about memorizing another keyword.

It is about precision in loop control.


The Core Meaning of continue

continue tells the loop:

“Stop this iteration now, and go back to the top.”

Execution does not exit the loop.

It jumps to the next repetition.


1. Simple Example — Excluding Multiples of 3

Goal

  • Print numbers from 0 to 20
  • 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 next iteration

The loop itself does not end.

Only this cycle is skipped.


Alternative Without continue

if (i % 3 != 0)
{
    Console.WriteLine(i);
}
Enter fullscreen mode Exit fullscreen mode

This works.

But the continue style expresses a different philosophy:

Filter invalid cases first, then let normal flow continue.

This is known as a guard clause style.


2. break vs continue — Precise Comparison

Keyword Current Iteration Entire Loop
break Stops Stops
continue Stops Continues

Code Comparison

break Example

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

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

Output: 0 to 4 only.

The loop terminates 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 to 9 except 5.

The loop continues.

Only that iteration is skipped.


3. Real Scenario — Protecting User Input

Recall this line:

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

If the user enters "abc":

  • FormatException
  • Program crashes

We want:

  • Ignore invalid input
  • Ask again
  • Prevent crash

Safe Version 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

If the user enters "abc":

  • isNumeric becomes false
  • continue executes
  • int.Parse is skipped
  • Loop restarts

The program does not crash.

This is controlled skipping.


4. Why break Did Not Cause Initialization Errors

Earlier observation:

break exits the loop completely.

So any uninitialized variable below is irrelevant.

But continue returns to the loop condition.

Therefore:

All variables used in the condition must remain valid.

This difference matters in real-world debugging.


Structural Understanding

  • break = structural escape
  • continue = local skip

Loops define repetition.

break exits repetition.

continue skips one cycle of repetition.


When continue Is a Good Choice

Use continue when:

  • There are many invalid cases
  • You want normal logic to remain clean
  • You want to filter early

Pattern:

if (invalidCondition)
{
    continue;
}

// normal logic here
Enter fullscreen mode Exit fullscreen mode

This keeps core logic uncluttered.


Mental Model You Must Build

continue does not terminate repetition.

It refines repetition.

It allows you to say:

“Ignore this case and move on.”


One-Line Summary

break exits the loop entirely.
continue skips the current iteration and proceeds to the next one.

Top comments (0)