0Pricing
C# Academy · Lesson

break and continue

Control loop flow.

break and continue is a free C# Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Controlling the Loop

Sometimes you don't want to process every item. C# gives you two keywords for this.

break stops the loop completely. continue skips to the next item.

What break Does

break exits the loop immediately. No more items are visited.

Use it when you found what you were looking for and there's no reason to keep going.

foreach (var n in nums)
{
    if (n == 0) break;
    Console.WriteLine(n);
}

break Example

This loop prints numbers until it hits a negative one, then stops.

Once break runs, the loop ends and code after the loop continues.

int[] nums = { 3, 7, -1, 9 };
foreach (var n in nums)
{
    if (n < 0) break;
    Console.WriteLine(n);
}
Console.WriteLine("stopped");

What continue Does

continue skips the rest of the current iteration and jumps to the next item.

The loop keeps running; it just ignores the work below continue for that one pass.

foreach (var n in nums)
{
    if (n < 0) continue;
    Console.WriteLine(n);
}

continue Example

Here we skip negative numbers but still process the rest.

Unlike break, the loop visits all items and only the negatives are skipped.

int[] nums = { 3, -2, 7, -5, 9 };
foreach (var n in nums)
{
    if (n < 0) continue;
    Console.WriteLine(n);
}

Searching With break

A classic use of break is searching. Stop as soon as you find a match.

This saves time because you avoid scanning the rest of the collection.

string[] users = { "sam", "ada", "leo" };
bool found = false;
foreach (var u in users)
{
    if (u == "ada") { found = true; break; }
}
Console.WriteLine(found);

Filtering With continue

continue is handy for filtering: skip items that don't qualify and act on the ones that do.

This sums only the positive values.

int[] nums = { 5, -3, 8, -1 };
int total = 0;
foreach (var n in nums)
{
    if (n < 0) continue;
    total += n;
}
Console.WriteLine(total);

break Stops Only One Loop

In nested loops, break exits only the loop it is inside.

The outer loop keeps running. The same is true for continue.

foreach (var row in grid)
{
    foreach (var v in row)
    {
        if (v == 0) break; // exits inner loop only
        Console.WriteLine(v);
    }
}

Combining Both

You can use break and continue in the same loop.

Here we skip zeros with continue and stop entirely at a negative with break.

int[] nums = { 4, 0, 6, -1, 8 };
foreach (var n in nums)
{
    if (n == 0) continue;
    if (n < 0) break;
    Console.WriteLine(n);
}

Choosing the Right One

Ask: do I want to stop the whole loop, or just skip this item?

Stop the loop → break. Skip one item and keep going → continue.

Code After the Loop

After break ends a loop, execution moves to the first line after the loop body.

continue never leaves the loop; it just begins the next iteration.

foreach (var n in nums)
{
    if (n == 5) break;
    Console.WriteLine(n);
}
Console.WriteLine("after loop");

Quick Check

Test your grasp of break and continue.

Recap

You learned that break exits a loop completely while continue skips to the next item.

In nested loops, each affects only the loop it sits in. Use break for searching and continue for filtering.

Frequently asked questions

Is the “break and continue” lesson free?

Yes — the full text of “break and continue” is free to read here on the web, and the C# Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO.

What will I learn in “break and continue”?

Control loop flow. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C# Academy?

No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “break and continue” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C# Academy lesson?

Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. The foreach Loop
  2. Iterating Collections
  3. break and continue
  4. Common Iteration Mistakes
← Back to C# Academy