Decision-Making Statements
You can have your script select a task to perform based on the conditions you specify. Using a decision-making statement, you can design the script to account for different possibilities or outcomes. This allows you to control the linear flow of what happens in your script.
The keywords for these statements are:
- IF: Selects which option to execute based on the result of the evaluation of a Boolean
expression.
- SELECT: Executes the first statement in a series of statements that evaluates as true.
- SWITCH:Evaluates an expression and compares the result to defined cases. It takes the action defined in the matching case.
Decision-making statements always require conditional code blocks. Blocks are enclosed in curly braces (
- CASE: Defines one of a set of possible statements to be executed.
- DEFAULT: Defines the default CASE. This is the statement that's executed if none of the other CASEstatements can be executed.
The discussion of each type of statement includes a syntax summary. These summaries use certain conventions to indicate the parts of the statement and whether each part is required or optional.
IF and IF... ELSE Statements
Description: Evaluates an expression and takes a branch based on the results of the evaluation.
Syntax:
Studio Action: If
Details: IF statements evaluate an <expression>. Depending on how the expression evaluates, the script executes the code in the first set of curly braces { ... }. The expression must evaluate as true or the script won't execute the code.
You can include the ELSE statement if you want to specify what the script does when the expression evaluates as false. ELSE is optional. Use it when there are two possible actions and you want the script to choose between them. Use IF alone when there's only one possible action and you want the script to perform it only when the specified condition (<expression>) is met.
Use a Boolean operator in the expression <expression> that's part of the IF statement. You must use an expression that evaluates to either true or false. For example, x=5 is a valid expression, because the value of the variable x will either equal 5 or it will not.
Studio does not support having more than one
Example
SELECT Statements
Description: Performs a set of commands based the first expression to evaluate to true.
Syntax:
Details: In a SELECT statement, the script evaluates each CASE <expression> starting at the top and working down until it finds the first that evaluates to true. For the first CASE <expression> to evaluate as true, SELECT executes the code within the curly braces [{ ... }] of that CASE <expression>.
If no CASE <expression> evaluates to true, the script executes the optional DEFAULT { ... } code.
At least one CASE statement is expected. If included, the DEFAULT { ... } statement must be the last clause in the list of CASE statements.
For example:
SWITCH Statements
Description: Evaluates the specified variable and takes the action defined by the matching CASE.
Syntax: SWITCH <var> <{CASE <literal> [{ ... }] [CASE <literal> [{ ... }]] [CASE ...] [DEFAULT [{ ... }]] }>
Details:
If no CASE <literal> matches <var>, the optional
SWITCH can only evaluate a variable. It cannot evaluate an expression. If you use an expression, your script will not work.
At least one CASE is expected. All CASE statements must have the same type of <literal>. The first CASE determines the type for the remaining CASE statements . If any CASE statement has a different type from the first CASE, a compiler error occurs. The following are all valid literal types:
- CASE "john"
- CASE 512
- CASE #"5/15/2050"
- CASE #"6/1/2050 7:00am"
- CASE #"7am"
For example:
If more than one CASE uses the same code { ... }, you don't need to include a code block after each one. Instead, list the CASE keywords one after the other, with the code after the last CASE that uses that code. This is called case fall-through. It's allowed as long as there is a case for SWITCH to encounter that does have a code block. The following example shows case fall-through. For CASEs 1, 2, and 3, the output is the value hi. For CASEs 5 and 6, the output is bye.
Use Case: SWITCH allows you to have specific outcomes depending on what the specified variable contains. For example, you can have the script evaluate a variable containing area codes. You can define a CASE for each possible area code and have the script add a value to the state variable that matches the state the area code is assigned to. For example:
CASE Statements
Description: Defines one of a set of possible statements to be executed. Used with SWITCH or SELECT.
Syntax: varies; see SWITCH or SELECT
Studio Action: Case
Details: CASE must be used in the conditional code block of a SWITCH or SELECT statement. Add one or more CASE statements to define the possible blocks for the script to execute. At least one CASE is required.
Related: Include a DEFAULT statement to define the case that's used if none of the specified CASE statements apply.
DEFAULT Statements
Description: Defines the CASE block that's used if none of the specified CASEs, apply. Used with SWITCH and SELECT.
Syntax: Varies; see SWITCH or SELECT
Details: DEFAULT must be used with SWITCH or SELECT blocks, and there must be at least one CASE statement present. DEFAULT cannot be used on its own. DEFAULT is always optional. It must be the last clause in the list of CASE statements.
ELSE Statements
Description: Defines the statement the script executes if the IF expression evaluates to false.
Syntax:
Details: ELSE is always optional. You can include an ELSE statement if you want to specify what the script does when the expression evaluates to false. Use it when there are two possible actions and you want the script to choose between them.