[Contents]
[Prev] [Next] [Limbo Basics] [Limbo Programming] [Language Definition]

The case Statement

The case statement transfers control to one of several places depending on the value of an expression:

labelopt case expression { qual-statement-sequence }

The expression must have type int or string. The case statement is followed by a sequence of qualified statements, which are statements labelled by expressions or expression ranges:

qual-statement-sequence:
	qual-list =>
	qual-statement-sequence qual-list =>
	qual-statement-sequence statement
	qual-statement-sequence declaration

qual-list:
	qualifier
	qual-list or qualifier

qualifier:
	expression
	expression to expression
	*

A qual-statement-sequence is a sequence of statements and declarations, each of which is preceded by one or more qualifiers. Syntactically the qualifiers are expressions, expression ranges with to, or *. If the expression mentioned after case has int type, all the expressions appearing in the qualifiers must evaluate to integer constants (see Constant Expressions). If the expression has string type, all the qualifiers must be string constants.

The case statement is executed by comparing the expression at its head with the constants in the qualifiers. The test is for equality in the case of simple constant qualifiers. In range qualifiers, the test determines whether the expression is greater than or equal to the first constant and less than or equal to the second.

None of the ranges or constants can overlap. If no qualifier is selected and there is a * qualifier, then that qualifier is selected.

Once a qualifier is selected, control passes to the set of statements headed by that qualifier. When control reaches the end of that set of statements, control passes to the end of the case statement. If no qualifier is selected, the case statement is skipped.

Each qualifier and the statements following it up to the next qualifier together form a separate scope, like a block; declarations within this scope disappear at the next qualifier (or at the end of the statement).

As an example, this fragment separates small numbers by the initial letter of their spelling:

case i {
1 or 8 =>
	sys->print("Begins with a vowel\n");
0 or 2 to 7 or 9 =>
	sys->print("Begins with a consonant\n");
* =>
	sys->print("Sorry, didn't understand\n");
}



[Contents]
[Prev] [Next] [Limbo Basics] [Limbo Programming] [Language Definition]

Copyright © 1998, Lucent Technologies, Inc. All rights reserved.