31. C# (continue)

Published: (March 10, 2026 at 02:43 PM EDT)
2 min read
Source: Dev.to

Source: Dev.to

0. The Essence of continue

continue immediately stops the current iteration and moves to the next one.

  • ❌ 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  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;   // skip parsing and restart loop
            }

            userNumber = int.Parse(input);

        } while (userNumber <= 10);

        Console.WriteLine("Loop ended.");
        Console.ReadKey();
    }
}

Execution Flow (user enters "abc")

isNumeric = false
continue executes
int.Parse is skipped
loop starts next iteration

Result: the program does not crash.


4. Why break Did Not Cause an Initialization Error

break exits the loop entirely, so any code after a break inside the loop is never reached. Consequently, the compiler does not require variables that would only be assigned after that point to be definitely assigned.

if (input == "stop")
{
    break;   // loop ends here
}

continue Is Different

continue jumps back to the loop condition, meaning the rest of the loop body is still part of the control flow. All possible execution paths must leave variables in a valid state, or the compiler will report errors.


5. When continue Is Useful

Use continue when:

  • Many invalid conditions exist
  • You want to filter early
  • You prefer cleaner control flow

Typical pattern:

if (invalidCondition)
{
    continue;   // skip to next iteration
}

// main logic below

This separates exception cases from normal execution.


Final Summary

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

Choose the keyword that matches the desired control‑flow behavior.

0 views
Back to Blog

Related posts

Read more »