The SELECT statement causes zero, one, or more of several
statements to be executed. The SELECTONE statement causes zero
or one statements to be executed. The execution depends on the
value of an ordinal expression called the select selector.
The SELECT and SELECTONE statements look much like the CASE
statement except for one very powerful feature. Namely, the
labels of a SELECT or SELECTONE statement can be run-time
expressions as opposed to the CASE statement which only allows
constant expressions.
Syntax:
SELECT select-selector OF
[[{{select-label-list},...: statement};...]]
[[ [[OTHERWISE {statement};...]]
[[ALWAYS {statement};...]] ]]
[[;]]
END
SELECTONE select-selector OF
[[{{select-label-list},...: statement};...]]
[[ OTHERWISE {statement};... ]]
[[;]]
END
The 'select-selector' is an expression of an ordinal type.
The 'select-label-list' is one or more select labels of the same
ordinal type as the select selector, separated by commas. A
select label can be a single expression, such as 1, or a range
of expressions, such as 5..10. The expressions in a
'select-label-list' can be full run-time expressions.
When two expressions are provided as a lower and upper bound,
they must be of the same ordinal type. There is no check to
ensure that the lower bound expression is less than or equal to
the upper bound expression. If that occurs then there are no
values of the select-selector that can be in the range.
The 'statement' is any statement to be executed depending on the
values of both the select-selector and the select-label.
The SELECT statement checks to see if the value of the
select-selector is contained in the select-label-list. If so,
then the corresponding statement is executed. The
select-label-lists are checked in the same lexical order that
they appear in the source file. The same value can appear in
more than one select-label-list. All corresponding statements
to select-label-lists are executed if the value is contained in
the select-label-list. By contrast, the SELECTONE statement
stops processing after it executes the first statement that
corresponds to a select-label-list that contains the
select-selector value.
The optional OTHERWISE and ALWAYS clauses can appear in either
order. The ALWAYS clause is always executed. The OTHERWISE
clause is executed only if none of the prior statements (except
for an optional ALWAYS statement) have been executed.
The syntax for the SELECTONE statement is almost identical but
does not provide for an ALWAYS clause.
1 – Examples
While the SELECT/SELECTONE statements can be used similar to the
CASE statement. For example,
SELECT expression OF
1: WRITELN('ONE');
2: WRITELN('TWO');
OTHERWISE WRITELN('not ONE or TWO')
END
a more subtle (and powerful) form uses the Boolean constant
'TRUE' as the select-selector. For example,
SELECTONE True OF
expression < 10: WRITELN('Value is small');
expression < 100: WRITELN('Value is medium');
expression < 1000: WRITELN('Value is big');
OTHERWISE WRITELN('Value is too big');
END
SELECTONE True OF
expression = "AAA": writeln('String is AAA');
expression = "BBB": writeln('String is BBB');
expression = "CCC": writeln('String is CCC');
OTHERWISE writeln('unknown string');
END
FOR i := 1 TO 10 DO
SELECT True OF
ODD(i): WRITELN('value ',i:1,' is odd');
(i MOD 3) = 0:
WRITELN('value ',i:1,' is also a multiple of 3');
END;