|Home | Tutorial | Classes | Functions | QSA Workbench | Language | Qt API | QSA Articles | Qt Script for Applications |

[Prev: Control Statements] [Home] [Next: case]

### break

    label:
    for ( var i = 0; i < limit; i++ ) {
        if ( condition ) {
            break label;
        }
    }

    switch ( expression ) {
        case 1:
            Statements1;
            break;
        default:
            DefaultStatements;
            break;
    }

This keyword is used in for loops, do loops, while loops and switch statements. When a `break` statement is encountered in a loop, control is passed to the statement following the end of the inner-most loop that contains the `break` statement; unless the `break` statement is followed by the name of a label, in which case control passes to the statement governed by the label.

A `break` statement is usually placed at the end of each case in a switch statement to prevent the interpreter "falling through" to the next case. When the interpreter encounters a `break` statement, it passes control to the statement that follows the inner-most enclosing `switch` statement. If every `case` has a corresponding `break`, then at most one `case`'s statements will be executed. If the `break` statement is followed by a label name (label) then when the `break` is encountered, control will pass to the statement marked with that label; this is useful, for example, for breaking out of deeply nested loops.

Example:

        red:
        for ( x = 0; x < object.width; x++ ) {
            for ( y = 0; y < object.height; y++ ) {
                if ( color[x][y] == 0xFF0000 ) {
                    break red;
                }
            }
        }

See switch for another example. See also do, while, for and break.

[Prev: Control Statements] [Home] [Next: case]