![]() |
![]() |
![]() |
![]() |
![]() |
The select
is a conditional statement that is used to branch program execution based on the value of an expression. The expression is evaluated and the resulting value is matched
against templates specified within the case
s. The execution continues with the statement block of the case
matching first.
Unlike to the if
statement where the way forward is determined with boolean conditions, in the select
statement the branching is determined using template matching.
It is important to mention that although the select
statement resembles to the alt
statement, these differ significantly. The evaluation of the select
statement does not involve snapshot handling thus it never blocks execution.
Related keywords:
select ( expression){ cases }; |
The select keyword introduces the conditional branch.
expression shall evaluate to a specific value.
cases contains one or more cases.
case ( matchings ){ statement_block }; |
The case keyword introduces the next case
matchings is a comma separated list of template references (including inline templates)
statement_block contains one or more statements. The block will only be executed if the value of select expression matches at least one template of this case.
case else { statement_block }; |
The case else may be used to specify the statements to be executed when no cases match.
case
s match and there is no case else
in the given select
statement then none of the statement blocks will be executed.
select (v) {
case (0) { log("zero") }
case ( (-infinity..-1) ) { log("negative") }
case ( (1..infinity) ) { log("positive") }
}
The above select statement performs sign check using inline value range templates and logs the result.
select (myMessage) {
case (tr_Message1, tr_Message2, tr_Message3) { log("1st") }
case (tr_MessageA, tr_MessageB) { log("2nd") }
case else { log("none") }
}
When the message stored in myMessage
variable matches templates tr_Message1
, tr_Message2
or tr_Message3
then the statement block of the first
case is executed. If myMessage
matches tr_MessageA
or tr_MessageB
then the second statement block is executed. Otherwise, when none of the enlisted templates
match myMessage
then the statement block of the case else
branch is executed.
BNF definition of select