Cicode Programming Reference > Working with Conditional Executors > Using the SELECT CASE statement

Using the SELECT CASE statement

The select case statement executes on several groups of statements, depending on the result of an expression. SELECT CASE statements are a more efficient way of writing code that would otherwise have to be done with nested IF THEN statements.

SELECT CASE Expression
CASE CaseExpression1,CaseExpression2
Statement(s);
CASE CaseExpression3 TO CaseExpression4
Statement(s);
CASE IS >CaseExpression5,IS<CaseExpression6
Statement(s);
CASE ELSE
Statement(s);
END SELECT

Where CaseExpressionn is any one of the following forms:

- expression
- expression TO expression

Where the TO keyword specifies an inclusive range of values. The smaller value needs to be placed before TO.

- IS <relop> expression.

Use the IS keyword with relational operators (<relop>). Relational operators that may be used are <, <=, =, <>, >, >= .

If the Expression matches any CaseExpression, the statements following that CASE clause are executed up to the next CASE clause, or (for the last clause) up to the END SELECT. If the Expression matches a CaseExpression in more than one CASE clause, only the statements following the first match are executed.

The CASE ELSE clause is used to indicate the statements to be executed if no match is found between the Expression and any of the CaseExpressions. When there is no CASE ELSE statement and no CaseExpressions match the Expression, execution continues at the next Cicode statement following END SELECT.

You can use multiple expressions or ranges in each CASE clause. For example, the following line is valid:

CASE 1 To 4, 7 To 9, 11, 13, Is > MaxNumber

You can also specify ranges and multiple expressions. In the following example, CASE matches strings that are exactly equal to "everything", strings that fall between "nuts" and "soup" in alphabetical order, and the current value of "TestItem":

CASE "everything","nuts" To "soup",TestItem

SELECT CASE statements can be nested. Each SELECT CASE statement needs to have a matching END SELECT statement.

For example, if the four possible states of a ship are Waiting, Berthed, Loading, and Loaded, the Select Case statement could be run from a button to display a prompt detailing the ship's current state.

select case iStatus
CASE 1
Prompt("Waiting");
CASE 2
Prompt("Berthed");
CASE 3
Prompt("Loading");
CASE 4
Prompt("Loaded");
CASE Else
Prompt("No Status");
END SELECT

See Also

Working with Conditional Executors