Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Turbo Pascal - <b> case selection control pp 58</b> http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
 CASE                         Selection Control                        pp 58


 Define:  The Case statement consists of an expression called the
          Selector and a list of statements, each preceeded by a case
          Label of the same type as the selector.

          A case Label consists of any number of constants or subranges
          separated by commas and followed by a colon.  A label may never
          be a variable.

 Purpose: Execute the statement whose case label matches the current value
          of the selector.

 Notes:   If there is no label/selector match, then none of the statements
          will be executed.  The optional Else clause may be included
          to execute additional statements if no label/selector match occurs.

          The Case statement provides a clean mechanism to implement
          multiple choices of the IF/THEN/ELSE variety.

 Usage:
          Procedure Proc1;  Begin End;        { Dummy procedure        }
          Procedure Proc2;  Begin End;        { Dummy procedure        }
          Procedure Proc3;  Begin End;        { Dummy procedure        }
          Procedure Proc4;  Begin End;        { Dummy procedure        }
          Procedure Fail ;                    { Out of range message   }
            Begin
            WriteLn ('Bad choice');
            End;

       TYPE
          State    =  (AK,AL,AR,AZ,CA,CO,CT,DE,DC,FL,GA,HI,ID,IL,IND,
                       IA,KS,KY,LA,MA,MD,ME,MI,MN,MO,MS,MT,NE,NV,NH,
                       NJ,NM,NY,NC,ND,OH,OK,ORE,PA,RI,SC,SD,TN,TX,UT,
                       VA,VT,WA,WI,WV,WY);

       CONST
          Selector :  Byte = 5;               { Selector out of range  }


       VAR
          Choice   :  State;                  { Selector = label type  }

       BEGIN
                    (* Ambiguous and hard to follow *)
          If Selector = 1 then Proc1          { Execute Proc1          }
            Else if Selector = 2 then Proc2   { Execute Proc2          }
            Else if Selector = 3 then Proc3   { Execute Proc3          }
            Else if Selector = 4 then Proc4   { Execute Proc4          }
            Else if Selector = 5 then Fail;   { Selector not 1..4 msg  }

                    (* Concise and easy to follow *)
          Case Selector of
            1 : Proc1                      ;  { Execute Proc1          }
            2 : Proc2                      ;  { Execute Proc2          }
            3 : Proc3                      ;  { Execute Proc3          }
            4 : Proc4                      ;  { Execute Proc4          }
            Else Fail                      ;  { Selector not 1..4 msg  }
            End;                           ;  { Of Case                }

                    (* Multiple case labels per statement *)
          Case Choice of
            CT,MA,ME,NH,
            RI,VT        :  WriteLn ('New England');

            DC,DE,MD,NJ,
            NY,PA        :  WriteLn ('Mid Atlantic');

            FL,GA,NC,SC  :  WriteLn ('South Eastern');

            IA,IL,IND,MI,
            MN,OH,WI,WV  :  WriteLn ('Mid Western');

            AL,AR,KY,LA,
            MO,MS,TN,VA  :  WriteLn ('Southern');

            KS,ND,NE,SD,
            WY           :  WriteLn ('Plains');

            AK,CA,CO,HI,
            ID,MT,ORE,UT,
            WA           :  WriteLn ('North Western');

            AZ,NM,NV,OK,
            TX           :  WriteLn ('South Western');
            End;
       END.

See Also: If/Then

Online resources provided by: http://www.X-Hacker.org --- NG 2 HTML conversion by Dave Pearson