The general types of selection statements in Limbo are: if
, case
, and alt
.
Note: See Channels in Chapter 3 for information aboutalt
.
if
statement selects statements to execute based on the evaluation of an expression. The two general formats of the if
statement are:
if (Theexpression
)statement
if (expression
)statement
elsestatment
expression
must produce an int
result. If the result is true (non-zero), then the first statement (or block) is executed. Otherwise, the statement (or block) following the else
is executed, if the else
clause is present.
if (guess == randnum) sys->print("Correct!\n"); else sys->print("Oh, sorry.\n");
if
statements can be nested. In Limbo, an else
statement always refers to the nearest if
statement that is within the same block as the else
and that is not already associated with an else
. For example:
if (i == j) { if (k == l) { ... } if (m == n) { ... } else { # this else is associated with ... # 'if (m == n)' } } else { # this else is associated with ... # `if (i ==j)' }
case
statement selects from multiple statements to execute based on the evaluation of an expression. The general form of the case
statement is:
caseTheexpression
{qualifier
=>statement
... }
expression
must produce an int
or a string
result. The qualifier(s)
can be simple constants, multiple constants, and ranges. They must evaluate uniquely, none of the ranges or constants may overlap. If there is no matching qualifier and a default qualifer, *, is present, then the default is selected.
Each qualifer and the statements that follow up to the next qualifier form a block. Each block breaks by default; no explicit break
statement is needed.
There is no "drop through" nature in the Limbo case
. A qualifier is not required to have statements, but execution does not continue through the next qualifier's statements.
The general syntax of a Limbo case
statement is similar to that of the C/C++ switch
statement. For example:
case direction { "north" or "east" => ... "south" => ... * => ...
case
statements can be nested. For example:
case x { 1 => case y { 0 => ... 1 => ... } 2 => ...