C Sharp

Branching with Jump Statements

Inside the embedded statements of any of the iteration statements covered in the previous sections, you can control the flow of execution with one of several statements collectively known as jump statements: break, continue, goto, and return.

The break Statement

You use the break statement to terminate the current enclosing loop or conditional statement in which it appears. Control is then passed to the line of code following that loop's or conditional statement's embedded statement. Having the simplest syntax of any statement, the break statement has no parentheses or arguments and takes the following form at the point that you want to transfer out of a loop or conditional statement: -

break -

In the following example, the application will print each number from 1 to 100 that is equally divisible by 6. However, when the count reaches 66, the break statement will cause the for loop to discontinue.

using System;
class BreakTestApp
{
    public static void Main()
    {
        for (int i = 1; i <= 100; i ++)
        {
            if (0 == i % 6)
            {
                Console.WriteLine(i);
            }
            if (i == 66)
            {
                break;
            }
        }
    }
}
}