C#: The break statement

The break statement is used to break from an enclosing do, while, for, or switch statement.

It is a compile error to use break anywhere else.

For example, consider the following code

using System;

namespace Conditional
{
    static class Program
    {
        static void Main()
        {
            int i = 0;
            while (true)
            {
                Console.WriteLine(i);
                i++;
                if (i > 3)
                {
                    break;
                }
            }
            Console.ReadLine();
        }
    }
}

The output from running the above code is 0,1,2,3 with the last number as 3.

It breaks the while loop when i is 4.

Series Navigation<< C#: For statement optional initialization
C#: The continue statement >>