0
0
C Sharp (C#)programming~5 mins

Break statement behavior in C Sharp (C#)

Choose your learning style9 modes available
Introduction

The break statement stops a loop or switch immediately. It helps you exit early when you find what you need or want to stop repeating.

When searching a list and you find the item you want, stop checking the rest.
When reading user input in a loop and the user types 'exit', stop the loop.
When processing options in a switch and you want to stop after one case runs.
When you want to avoid unnecessary work after a condition is met inside a loop.
Syntax
C Sharp (C#)
break;

The break statement must be inside a loop (for, while, do-while) or a switch block.

It immediately exits the nearest enclosing loop or switch.

Examples
This loop prints numbers 0 to 4. When i is 5, break stops the loop.
C Sharp (C#)
for (int i = 0; i < 10; i++)
{
    if (i == 5)
        break;
    Console.WriteLine(i);
}
Each case ends with break to stop checking other cases.
C Sharp (C#)
switch (day)
{
    case "Monday":
        Console.WriteLine("Start of week");
        break;
    case "Friday":
        Console.WriteLine("End of week");
        break;
    default:
        Console.WriteLine("Midweek");
        break;
}
Sample Program

This program shows how break stops a loop early and ends a switch case.

C Sharp (C#)
using System;

class Program
{
    static void Main()
    {
        Console.WriteLine("Numbers until 5:");
        for (int i = 0; i < 10; i++)
        {
            if (i == 5)
                break;
            Console.WriteLine(i);
        }

        Console.WriteLine("\nSwitch example:");
        string day = "Friday";
        switch (day)
        {
            case "Monday":
                Console.WriteLine("Start of the week");
                break;
            case "Friday":
                Console.WriteLine("End of the week");
                break;
            default:
                Console.WriteLine("Midweek day");
                break;
        }
    }
}
OutputSuccess
Important Notes

Without break in a switch, execution continues to the next case (called fall-through), which is usually not wanted.

Using break helps avoid unnecessary work and can improve program speed.

Summary

break stops the nearest loop or switch immediately.

Use it to exit early when a condition is met.

Always place break; inside loops or switch cases.