Homework 10 - October 25

Multi-alternative selection statements

 

  1. What do each of the following languages call their multi-alternative selections statements?
    1. Pascal  case
    2. Java   switch
    3. Ada     case

 

  1. What types are allowed for the multi-alternative variable in
    1. Pascal  ordinal value
    2. Java    char, int, short, byte all integer types but long
    3. Ada    any discrete type

 

  1. Each of the multi-alternative choice statements has two parts:  a value and  an action.   Provide a simple example of a choice statement in
    1. Pascal  case limit of

                1,2,3 :     ;

                 4,5,6 :  writeln (‘hello’);

                end;

    1. Java   switch (digit)

                         {

                            case 1: System.out.print(“one”); break;

                            case 2 : System.out.print(”two”); break;

                            default : System.out.print (“error”);

                          }

    1. Ada  case number is

                             when 1 =>  x := x + 1;

                             when 2 => x := x + 2;

                             when others => null;

                            end case;

 

  1. Is the multi-alternative choice statement allowed to have multiple values in
    1. Pascal  yes see 3a
    2. Java   no
    3. Ada    yes

 

  1. If your answer to the previous question was "yes" give an example the value part of such a choice statement in
    1. Pascal   see 3a
    2. Java      no answer required here
    3. Ada     case number is

                             when 1 | 2 => ada.integer_text_io.put (number);

                             when others => null;

                            end case;

 

  1. Can a value appear in more than one of the choice statements in
    1. Pascal  no
    2. Java  no
    3. Ada   no

 

  1. Is a "default" choice provided and if so, what is its format in
    1. Pascal  no, not in Standard Pascal  yes, in Turbo Pascal  else writeln (“hi”);
    2. Java   default  : ...;
    3. Ada    when others => ... ;

 

  1. How does the behavior of the multi-alternative choice statement in Pascal and Ada differ from that in C and Java?

In Pascal and Ada, once a match occurs, only that choice action is executed.  In C and Java, all subsequent choice actions are executed unless a break occurs.

 

  1. What selection statement in FORTRAN  is most similar to the multi-alternative choice statement in Ada, Java and Pascal?

     The computed goto

 

  1. Is there anything distinctive about Ada's multi-alternative choice statement when compared to that of Java and Pascal?

 Ada requires that all possible values of the case variable be accounted for.  Java and Pascal do not.