Equinox Online Help - Language Reference - A to Z

Home

Switch ... End Switch

Applies to
Syntax
switch TestExpression
[statements]
case Case1Expression
case statements
case Case2Expression
case statements
case Case3Expression
case statements
[Exit Switch]
[Continue Switch]
...
default
[default statements]
end switch

Note that you may use EndSwitch instead of End Switch.

Action[Statement] The test expression is evaluated. The result determines which statements are executed.
ScopeUsable anywhere.
Notes

The Switch statement is a simple and powerful way of writing conditional statements. Each Case expression is compared with TestExpression in turn. If a match is found, then the associated statements are executed and control passes to the line following the End Switch statement. If no Case expressions match TestExpression then any statements associated with the Default statement are executed.

Exit Switch causes control to pass to the line following the End Switch statement. Continue Switch branches to the evaluation of the Switch statement again.

The Switch statement can greatly simplify complex conditional testing. eg The syntax example is equivalent to the following If structure:

number TempVar | declare TempVar

TempVar = expression

statements

if TempVar = Case1Expression then
case statements
else if TempVar = Case2Expression then
case statements
else if TempVar = Case3Expression then
case statements
...
else
default statements
end if

You may attach more than one expression to a single Case statement using a list expression, eg:

case case1expression, case2expression.

If you do not include comparative operator (eg >) then, equals is assumed. You can also use the To operator to specify a range of values, eg:

case case1expression to case2expression

There is a special form of the Switch statement, which you can use on a form to identify the current field:

switch Field
case Field1Name
statements
case Field2Name, Field3Name
statements
end switch

The special keyword Field matches the name of the current field. Note that you may only use field names in Switch Field statements.

CategoryFlow Control
See Also Block_EndBlock, BreakPressed, Continue, Execute, EnableInput, Exit, For...Next, Goto, If_Then, Repeat_Until, Return, Sleep, Subtable, Switch, While_EndWhile
Example

The following example method chooses a type of birthday card, depending on the age of the recipient. If the person's age is listed in the first Case statement, a special card is sent. Otherwise, children less than ten years old get a child's card and so on.

switch age
case 1, 16, 18, 21, 40, 100
CardType = "SPECIAL"
case < 10
CardType = "CHILD"
case 30 To 39
CardType = "AGEIST"
default
CardType = "NORMAL"
end switch

Note that an age of one is true for the first and second case expressions. Only the first case would actually be executed.