← Back to C# Course | Chapter 4: Control Flow | Lesson 7 of 7

break and continue

In this page:

  1. break and continue

break and continue

break immediately exits the nearest enclosing loop or switch, while continue skips the rest of the current iteration and jumps to the next one. Both give you finer control inside loops beyond what the loop condition alone provides. Overusing them can make control flow harder to follow, so use them sparingly.

Note: Use continue to skip invalid items early instead of nesting the rest of the loop body in an if.

Example: break and continue

markup
using System;

class Program
{
    static void Main(string[] args)
    {
        for (int i = 1; i <= 10; i++)
        {
            if (i % 2 == 0)
                continue;   // skip even numbers

            if (i > 7)
                break;      // stop once we pass 7

            Console.WriteLine(i);
        }
    }
}
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.