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

[Prev: return] [Home] [Next: throw]

### switch

    switch( expression ) {
        case Value :
            Statements;
            break;
        default :
            DefaultStatements;
            break;
    }

A `switch` statement provides a multi-way branch. The expression is evaluated once, then each case is checked in order to find one with a Value that matches the value of the expression. If a match is made, the Statements of the matching case are executed, after which control passes to the statement following the `switch` block. If no matching case is found, and there is a default case, the DefaultStatements are executed, after which control passes to the statement following the `switch` block. If there is no matching case and no default, no statements within the `switch` block are executed, and control passes to the statement following the `switch` block.

Note that if a default is used it must be come after all the cases; this is because once default is encountered it is treated as a matching case regardless of what follows.

Every case, and the default (if used) should have a break as their last statement. If break is not present, control will "drop through" to the following statements, which is not usually the desired behavior.

The expression may be any arbitrary Qt Script for Applications expression that evaluates to an object that can be strictly compared. For example, an expression that evaluates to a Boolean, Date, Number or String value.

Example:

In the example, if `expr` has the value 1, the doActionOne() function will be called. But if `expr` has the value 3, both doActionThree() and doActionFour() will be called, because case 3 doesn't have a `break` statement, and execution "falls through" to case 4. If `expr` is not 1, "two", 3 or 4 then the default case will be matched and doDefaultAction() will be executed.

See also break.

[Prev: return] [Home] [Next: throw]