Export (0) Print
Expand All

break Statement (C++)

The break statement ends execution of the nearest enclosing loop or conditional statement in which it appears. Control passes to the statement that follows the ended statement, if any.

break;

The break statement is used with the conditional switch statement and with the do, for, and while loop statements.

In a switch statement, the break statement causes the program to execute the next statement after the switch statement. Without a break statement, every statement from the matched case label to the end of the switch statement, including the default clause, is executed.

In loops, the break statement ends execution of the nearest enclosing do, for, or while statement. Control passes to the statement that follows the ended statement, if any.

Within nested statements, the break statement ends only the do, for, switch, or while statement that immediately encloses it. You can use a return or goto statement to transfer control from more deeply nested structures.

The following example illustrates the use of the break statement in a for loop.

// break_statement.cpp

#include <stdio.h>

int main()
{
    int i;

    for (i = 1; i < 10; i++)
    {
        printf_s("%d\n", i);

        if (i == 4)
            break;
    }
}  // Loop exits after printing 1 through 4
1
2
3
4

Community Additions

ADD
Show:
© 2014 Microsoft