Loading...

C# break and continue

Loop i from 1 to 5. When i equals 3, leave the loop with break before you write. The screen should show only two lines: 1, then 2. That early stop is one tool on this page. The other tool is continue, which skips one pass and then keeps looping so 3 never writes but 4 and 5 still do. Keep the public class named Pine.

break ends the loop early

Open the Pine file. The class name stays Pine. This page does not rename the type. Delete any plain while that only counts if that shape is still there. Delete a plain for without an if that stops early if you want this page’s demos alone. Delete any ReadLine line. The file you keep has no wait for typing, and the screen is written only by the writing line that runs when the if is false.

Inside Main, write a for that can stop early:
for (int i = 1; i <= 5; i = i + 1) {
if (i == 3) {
break;
}
Console.WriteLine(i);
}
The for header is the same shape you already know: start, test, step. Inside the braces, an if asks whether i equals 3. When that is true, break; runs. break leaves the whole for loop at once. The writing line under the if does not run for that pass, and i never reaches 4 or 5 inside this loop. When i is 1 or 2, the if is false, so Console.WriteLine(i) writes the number. Save the file.

The file you keep

Keep this teaching shape. One for from 1 to 5. One if (i == 3) that runs break;. One writing line after the if that writes the bare name i. There is no continue in the file you keep, and no quotes around break.

break when i is 3

csharp
using System;

public class Pine
{
    public static void Main()
    {
        for (int i = 1; i <= 5; i = i + 1)
        {
            if (i == 3)
            {
                break;
            }
            Console.WriteLine(i);
        }
    }
}

Read it from the top. using System; prepares short names. public class Pine names the type. public static void Main() is where the runtime starts. The for walks i from 1 toward 5. When i is 3, break; ends the loop. Two lines on the screen: 1, then 2. No third line.

Run it with dotnet run. The program writes two lines and stops. It does not wait for you to type. If dotnet run waits, ReadLine is still in the file. Delete that line and keep the for with break above. The screen shows 1, then 2. If the screen shows five lines, the break is missing or the if never matches. If the screen shows the word break as letters, a writing line has quotes around that word. You do not reinstall the .NET SDK for a screen that shows the wrong lines.

Walk the break run in order. i starts at 1. The if is false, so the writing line writes 1. The step stores 2. The if is false, so the writing line writes 2. The step stores 3. Now i == 3 is true, so break runs and the for ends. Nothing writes for 3, 4, or 5. A plain loop without break would have written five lines; break cut the loop short after two writes. The step never runs after break on that pass, and the test is not asked again, because the loop is already over.

continue skips one pass

Change the body so continue replaces break, and keep the writing line after the if:
for (int i = 1; i <= 5; i = i + 1) {
if (i == 3) {
continue;
}
Console.WriteLine(i);
}
When i equals 3, continue; skips the rest of that pass — including the writing line — and the for moves on to the step and the next test. The loop itself does not end. The screen should show four lines: 1, 2, 4, and 5. The hole is where 3 would have been.

continue when i is 3

csharp
using System;

public class Pine
{
    public static void Main()
    {
        for (int i = 1; i <= 5; i = i + 1)
        {
            if (i == 3)
            {
                continue;
            }
            Console.WriteLine(i);
        }
    }
}

This is the second file. It is not the file you keep. Run dotnet run. The screen shows four lines. You did not change the for header — only break became continue. After you have seen four lines, put the kept file back: break inside the if. Both versions are valid C#. They teach two different early moves inside the same loop shape.

Read the continue run once more. i goes 1, 2, 3, 4, 5. For 1 and 2 the if is false and the numbers write. For 3 continue skips the writing line. For 4 and 5 the numbers write again. The loop still finishes its normal stop when i becomes 6. continue never ends the for; it only skips the rest of one pass. After continue, the for still runs its step i = i + 1, so the counter keeps moving even though that pass did not write.

📊 tool and the screen

Tool at i == 3 Screen
break 1 then 2
continue 1 then 2 then 4 then 5

Use the table as a check after a run, not instead of one. break leaves two lines. continue leaves four lines with 3 missing. If 3 still appears when you meant continue, the writing line is probably above the if — move Console.WriteLine(i) below the continue so the skip can reach it. Fix the order rather than reinstalling the .NET SDK.

Rules that keep the two tools clear

break and continue belong inside a loop body. Writing break; alone in Main with no enclosing for or while makes the build write an error. The same is true for continue;. On this page an if chooses the moment: when i equals 3. You can write other tests later; the demos keep i == 3 so the screen change is easy to see.

break is not continue. break ends the loop. continue skips one pass and stays in the loop. The break inside a switch on an earlier page ends a case; here the enclosing structure is a for. Do not leave a plain while that only counts, or a plain for without an if, in the same Main and wonder why every number from 1 to 5 still writes. This session’s Main should hold the for plus the if with break or continue.

A common mix-up is writing the word itself: Console.WriteLine("break");. That writes the characters break. It does not stop a loop. Another mix-up is putting Console.WriteLine(i); before the if that holds continue, so 3 still writes and then continue runs with nothing left to skip. Keep the writing line after the if in both demos. If you write continue with no enclosing loop, the build writes an error; the same happens for a lone break outside any loop. These two words only make sense inside a loop body on this lesson’s shape.

You can now put break inside a loop to leave early, or continue to skip one pass and keep going. An if can choose when that happens. The next page is foreach, where a loop walks each value in a collection without writing the start, test, and step by hand the same way.

The screen is the early move

The file you keep writes 1, then 2. The word break is in the file, and break is not on the screen. The second file writes four lines with 3 missing. Quotes around break write letters, not an early stop.

🧠 Test Your Knowledge

Ready to Start

Test Your Knowledge

Challenge yourself with this interactive quiz and see how well you understand the topic

❓
5
Questions
🎯
70%
To Pass
♾️
∞
Time
🔄
∞
Attempts

📝 Instructions

  • Read each question carefully
  • Select the best answer for each question
  • You can retake the quiz as many times as you want
  • Your progress will be shown at the top