switch | |||||||||||||||||||
Type | control structure | ||||||||||||||||||
Dictionary | LCS | ||||||||||||||||||
Library | LiveCode Script | ||||||||||||||||||
Syntax |
| ||||||||||||||||||
Summary | Chooses among several possible values for an expression, and executes a set of statements that depends on the value. | ||||||||||||||||||
Introduced | 1.0 | ||||||||||||||||||
OS | mac, windows, linux, ios, android | ||||||||||||||||||
Platforms | mobile | ||||||||||||||||||
Parameters |
| ||||||||||||||||||
Related | Keyword: default, case, card, end switch Function: value, commandNames Glossary: current card, value, execute, statement, keyword, expression, control structure, evaluate, command Control Structure: switch, if, break, repeat, try | ||||||||||||||||||
Description | Use the switch control structure to select among multiple possible conditions, performing a different set of actions for each possibility. Form: The switch control structure begins with the word switch on a single line, with an optional switchExpression. The switch line is followed by one or more case sections. Each case section begins with the case keyword, followed by either a caseValue (if a switchExpression was included on the switch line) or a caseCondition (if no switchExpression was included). If the caseValue is equal to the switchExpression, or the caseCondition evaluates to true, LiveCode begins executing the following statements. The case sections may be followed by an optional default section. If no break statement has been encountered yet in the switch control structure, the statements in the default section are executed. The switch structure ends with an end switch statement. Flow of control in a switch structure is less complicated than it looks. In general, when LiveCode enters a switch control structure, it looks for the first case section whose caseValue is equal to the switchExpression, or whose caseCondition is true. When a matching condition is found, all statements following it are executed--even statements in another case section--until either a break statement is encountered or the switch control structure ends. This means that if you do not end a case section's statementList with a break statement, the statements in all the following case sections (and the default section) are executed even if those case sections don't have a matching caseValue or a true caseCondition. Occasionally, this behavior is useful. However, in most cases, you should place a break statement at the end of each statementList. This ensures that only one statementList is executed, and the rest are skipped. This also means that you can attach more than one caseValue or caseCondition to the same statementList, simply by placing one case line above the next. The following example beeps if the current card is either the last or first card, and goes to the next card otherwise:
There is no limit to the number of case sections you can include in a switch control structure, although the more case sections there are, the more expressions LiveCode must evaluate and the more slowly the switch structure executes.
|