Tutorial - 'Select' Statement

The If/Then/Else statement is very versatile but if there are a lot of simple conditions it can get very long-winded. There is an alternate construct to handle these cases.

Select Statement

The overall form of a Select statement :-

Select expression From
  [[ Case options Do statement_list ]]
  [ Otherwise statement_list ]
EndSelect ;

Where the options is a comma-separated list of either a value or a range value To value.

The case label options are tested in turn and when one matches the value of the expression the associated statement list is executed. Once one list is executed the statement terminates. The values and ranges may overlap but only the first match result in the statements being executed. If there are no matches then the "Otherwise" list ( if there is one ) will be executed. All the values for the cases must be constants calculable at compile time.

The range cases use a comparison, and as we already know this will generate an error if the expression yields null. To avoid this have the first case as an explicit test for Null.


Shape PlanetSymbol(Number n, Number size)
    Begin
	Select n From
	    Case Null Do
		Output "Error: Null planet number";
	    Case 1 Do
		MercurySymbol(size);
	    Case 2 Do
		VenusSymbol(size);
	    Case 3 Do
		EarthSymbol(size);
	    Case 4 Do
		MarsSymbol(size);
	    Case 5 To 8 Do
		ErrorSymbol(size);
		Output "Gas giant symbols not implemented";
	    Otherwise
		ErrorSymbol(size);
		Output "Bad planet number", n;
	EndSelect;
    End;
stmt_select_1.grs Output of stmt_select_1.grs stmt_select_1.png