touque.ca > Education Commons > Programming: Program control > Selection (II) > Repetition

Program control

Selection (II)

An if statement of Form 3 can become awkward when there are more than three or four alternatives. For selection amongst more alternatives, a case statement is often best.

case statement

A case statement begins with the keyword case, a control value, and the keyword of. The control value is an integer, character, or string; it may be a constant, but usually the control value is an integer, character, or string variable. (Other control-value datatypes are possible, but they are not discussed here.)

The body of the case statement is composed of one or more sets of statements which are to be executed. Each set is labelled with the keyword label, a list of values of the same datatype as the control value, and a colon; and each set is composed of zero or more statements. The list may contain zero or more values.

A case statement ends with the keywords end case.

For example, in a program which displays the room in which grade representatives are to meet, there are four alternatives: one for each grade. In Example 1, the user enters the grade of interest and the corresponding room number is displayed. Note that the control value and the label values are all integers.

% Example 1:

get grade

put "Your meeting is in room " ..
case grade of
   label 9:
      put "C3."
   label 10:
      put "C13."
   label 11:
      put "235."
   label 12:
      put "218."
   label:
      put "unknown! Please re-enter grade."
end case

Example 2 tallies the votes for each of the registered political parties in Ontario. Note that the control value and the label values are all strings.

% Example 2:

case vote of
   label "CPC":
      Communist := Communist + 1
   label "FCP":
      FamilyCoalition := FamilyCoalition + 1
   label "FP":
      Freedom := Freedom + 1
   label "GP":
      Green := Green + 1
   label "ONDP":
      NewDemocratic := NewDemocratic + 1
   label "NOHP":
      NHeritage := NHeritage + 1
   label "OLP":
      Liberal := Liberal + 1
   label "OL":
      Libertarian := Libertarian + 1
   label "OCOR":
      OntarioCOR := OntarioCOR + 1
   label "PPSN":
      SpecialNeeds := SpecialNeeds + 1
   label "PFRPO":
      Republican := Republican + 1
   label "PCP":
      Conservative := Conservative + 1
   label "RPO":
      Reform := Reform + 1
   label:
      spoiled := spoiled + 1
end case

Example 3 reports the airport gate for destinations in three provinces. Note that the number of label values varies for each province.

% Example 3:

case destination of
   label "YHM", "YOW", "YYZ":
      % Ontario destinations
      gate := "gate 15."
   label "YQB", "YUL":
      % Quebec destinations
      gate := "gate 37."
   label "YLW" "YQQ", "YVR", "YYJ":
      % B.C. destinations
      gate := "gate 6."
   label:
      % unknown destination
      gate := "information desk."
end case

put "Please proceed to ", gate

touque.ca > Education Commons > Programming: Program control > Selection (II) > Repetition