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.

Read the syntax

break exits the nearest enclosing loop immediately. continue skips the rest of the current iteration and proceeds with the next iteration of that loop.

Use these statements where they make the exit or skip condition clearer than deeply nested if blocks. They do not exit an entire method; return does that.

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");
}
  1. Exiting a loop as soon as a target condition is found.
  2. break exits the nearest enclosing loop immediately. continue skips the rest of the current iteration and proceeds with the next iteration of that loop.
  3. The loop prints that 1, 3 and 5 are odd, then encounters 8. It prints Found first even number: 8 and break prevents any later values from being checked.

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
}
  1. Skipping specific iterations while letting the loop continue.
  2. break exits the nearest enclosing loop immediately. continue skips the rest of the current iteration and proceeds with the next iteration of that loop.
  3. The remainder test is true for odd i values, so continue skips their WriteLine. The output contains 2, 4, 6, 8 and 10.

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}");
}
  1. Using both together to filter and stop early within the same loop.
  2. break exits the nearest enclosing loop immediately. continue skips the rest of the current iteration and proceeds with the next iteration of that loop.
  3. Short passwords are skipped; password123 and hunter2! are reported as checked. Reaching SecureP@ss1 prints the target message and exits before examining more entries.

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

Runtime & versions

.NET runtime. Feature availability depends on the language and runtime version used by your project; consult the official reference below.

C# language reference

Specifications & further reading