2.05 Selection

Select

A select construct may be used to carry out a multi-way selection.

Function:

select starts a multi-way decision block and optionally sets up the expression that subsequent when statements are tested against. If no expression is provided then the default is for all subsequent tests to be true.

If all is specified any of the subsequent cases that are true will be executed.

The select block is an optimised if …. then … else construct. It works in a similar way. See 2.04 Conditional Execution.

Syntax:

select [case] [[as] const] [all] [test-expression]

when expression[, …][:]
:
when [else|all][:]
:
end select

where:

case
Used to identify a value to be tested. This is optional and is included for historical reasons.
as const
When as const is used, the expressions specified in subsequent when statements must all resolve to unique integer constants. This provides a much faster search.
all
If all is specified, each subsequent test that matches will be executed.
test-expression
A valid LynPlex expression that will be used to test each case. If this is provided, each expression in a when statement is tested against this value, otherwise the expression will be tested for a non-zero result.
when
Used to identify a test. The word case may also be used.
expression
anything that is a valid expression in LynPlex. In addition it can be a range of values in the form:
value1 to value2
or it can be a comparison expression in the form:
is operator value
where operator is any of the logical operators.

The when statement may be terminated by a colon (:) and additional statements can then be entered on the same line.

Note that LynPlexC does not yet support the terminating colon. For the time being it is necessary to use a semi-colon instead.

Processing:

In any single when statement multiple values may be specified by separating them with commas:

when value1, value2, …

This has the same effect as an if statement in the format:

if test = value1 or test = value2 or

Each expression in the subsequent when statements is compared with the test expression. If any case matches the test expression, the statements following the when statement are executed up to the next when or end select statement.

If no expression is specified on the select statement, an expression in a when statement needs to resolve to a non-zero numeric value or a string that is not empty.

If a when else statement is present, the statements within the else block will be executed if no previous when statement has been matched.

If the all option is used, when else is ambiguous and the subsequent statements may or may not be executed depending on the implementation. In this case the word all may be used instead. The statements within the all block will always be executed.

End Select is used to mark the end of a select block.

Exit Select

Exit Select transfers execution past the matching end select statement, bypassing any when else or when all statements.

See also: exit from loops

Next Case

Next Case transfers execution directly to the next when statement where execution continues. The next and subsequent when blocks are tested and then executed only if one of its case expressions matches the original test expression.

As both compilers and the interpreter evaluate the expression at the start of the select block, changing a variable that is part or all of the original test expression will not alter the results of any subsequent tests.

Example:

put "ix =" ix
select ix
  when a:     put "  a = ix"
  when b,c,d: put "  b, c or d = ix"
  when b+c:   put "  b+c = ix"
  when else:  put "  no match in the above statements"
    select all ix
      when is < 2: put "    ix is less than 2"
      when 3 to 5: put "    ix is between 3 and 5"
      when is = 4: put "    ix is equal to 4"
      when 5:      put "    ix is equal to 5"
      when is > 8: put "    ix is greater than 8"
      end select
  end select

Implementation Differences:

LynPlex Interpreter
exit select and next case are not yet implemented.
as const is not supported.
LynPlexC Compiler
Fully implemented.
: at end of when statement is not yet supported.
LynPlexS Compiler
as const is not yet implemented.

lynplex/lp0205.txt · Last modified: 2011/11/06 19:35 (external edit)