Compartilhar via


The break Statement 

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

break;

Remarks

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

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

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

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

Example

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

Output

1
2
3
4

See Also

Reference

Jump Statements (C++)
C++ Keywords
The continue Statement