Error getting tags :
error 404Error getting tags :
error 404 switch | revDocs | RunRev
Welcome Guest (Log in)
Product Edition
Version
switch
Basics
Expander triangle
Type
Control Structure
Syntax
switch [switchExpression] case {caseValue | caseCondition} [statementList] [default defaultStatementList] end switch
Introduced
1.0
Environment
Desktop, Web and Server
Platform Support
MacOS,Mac OS X,Windows,Linux
Security
None required
Summary
Chooses among several possible values for an expression, and executes a set of statements that depends on the value.
Examples

Additional Comments
Expander triangle

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, Revolution 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.

Parameters:

The switchExpression is any expression.

The caseValue is any expression. (If the caseValue evaluates to the same value as the switchExpression, the condition is matched for that case section.)

The caseCondition is any expression that evaluates to true or false. (If the caseCondition evaluates to true, the condition is matched for that case section.)

Each statementList consists of one or more Revolution statements, and can also include if, switch, try, or repeat control structures.

The defaultStatementList consists of one or more Revolution statements.

Comments:

Flow of control in a switch structure is less complicated than it looks. In general, when Revolution 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:

switch (the number of this card)

case 1

case (the number of cards)

-- both the above case conditions execute the following

-- statements:

beep

break

default

go next card

end switch

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 Revolution must evaluate and the more slowly the switch structure executes.

Note: The switchcontrol structure is implemented internally as a command and appears in the commandNames.

User Comments
Expander triangle