Codectionary / Developer documentation / C#

break and continue

break and continue give fine-grained control over loop execution. break immediately exits the nearest enclosing loop (or switch statement) entirely, skipping any remaining iterations. continue skips just the rest of the current iteration and jumps straight to the loop's next condition check, without exiting the loop.

Syntax

break;\ncontinue;

Examples

Using break

Exiting a loop as soon as a target condition is found.

int[] numbers = { 1, 3, 5, 8, 9, 12 };

foreach (int num in numbers)
{
    if (num % 2 == 0)
    {
        Console.WriteLine($"Found first even number: {num}");
        break;
    }
    Console.WriteLine($"{num} is odd");
}

Using continue

Skipping specific iterations while letting the loop continue.

for (int i = 1; i <= 10; i++)
{
    if (i % 2 != 0)
    {
        continue;  // skip odd numbers
    }
    Console.WriteLine(i);  // prints only even numbers
}

Combining break and continue

Using both together to filter and stop early within the same loop.

string[] passwords = { "abc", "password123", "hunter2!", "12345", "SecureP@ss1" };

foreach (string pwd in passwords)
{
    if (pwd.Length < 8)
    {
        continue;  // skip anything too short
    }

    if (pwd == "SecureP@ss1")
    {
        Console.WriteLine("Found the target password!");
        break;
    }

    Console.WriteLine($"Checked: {pwd}");
}

Best practices

  • Use break to stop a loop as soon as further iteration is pointless, avoiding wasted work
  • Use continue to skip invalid or irrelevant items cleanly instead of wrapping the rest of the loop body in a large if block
  • In nested loops, remember break and continue only affect the innermost loop they are directly inside - use a labeled goto only in rare cases where that limitation genuinely matters
  • Prefer restructuring logic into a separate method with an early return over deeply nested break/continue when readability suffers

At a glance

Purpose
Applications on the .NET platform
File extension
.cs
Runs in
.NET runtime
Usually used with
.NET SDK and libraries

Specifications & further reading

Related C# documentation

Variables & Data Types
C# is a statically-typed language, meaning every variable's type is fixed at compile time. Built-in value types include int, double, decimal, bool, and char, while string and object are reference types. The var keyword lets the compiler infer a variable's type from its initializer, but the variable is still strongly typed underneath - it just saves you from writing the type name explicitly.
Console.WriteLine / ReadLine
The Console class, from the System namespace, provides the standard way to read from and write to the terminal in a C# console application. Console.WriteLine() prints a value followed by a newline, Console.Write() prints without one, and Console.ReadLine() reads a full line of text typed by the user, always returning it as a string.
Comments & XML Documentation
C# supports single-line comments with //, multi-line comments with /* */, and a special triple-slash XML documentation format (///) placed above a member. XML doc comments support tags like <summary>, <param>, and <returns>, and are used by Visual Studio and other IDEs to power IntelliSense tooltips and can be compiled into full API documentation.
Arithmetic & Assignment Operators
C# provides the standard arithmetic operators for numeric computation: +, -, *, / for division, and % for the remainder. Integer division truncates any decimal part, just as in many C-family languages. Compound assignment operators (+=, -=, etc.) combine an operation with assignment, and increment/decrement operators (++, --) come in prefix and postfix forms that differ subtly in when the value updates relative to being used.