Homework 10 - October 25
Multi-alternative selection statements
- What do each of the following
languages call their multi-alternative selections statements?
- Pascal case
- Java
switch
- Ada
case
- What types are allowed for the
multi-alternative variable in
- Pascal ordinal value
- Java char, int,
short, byte all integer types but long
- Ada
any discrete type
- Each of the multi-alternative choice
statements has two parts: a value
and an action. Provide a simple example of a
choice statement in
- Pascal case limit of
1,2,3 : ;
4,5,6 : writeln
(‘hello’);
end;
- Java
switch (digit)
{
case 1: System.out.print(“one”); break;
case 2 : System.out.print(”two”);
break;
default :
System.out.print (“error”);
}
- Ada
case number is
when 1 => x := x +
1;
when
2 => x := x + 2;
when
others => null;
end
case;
- Is the multi-alternative choice
statement allowed to have multiple values in
- Pascal yes see 3a
- Java
no
- Ada yes
- If your answer to the previous
question was "yes" give an example the value part of such a
choice statement in
- Pascal see 3a
- Java
no
answer required here
- Ada case number
is
when 1 | 2 => ada.integer_text_io.put (number);
when
others => null;
end
case;
- Can a value appear in more than one of
the choice statements in
- Pascal no
- Java
no
- Ada no
- Is a "default" choice
provided and if so, what is its format in
- Pascal no, not in Standard Pascal yes, in
Turbo Pascal else writeln (“hi”);
- Java
default :
...;
- Ada when others
=> ... ;
- 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.
- What selection statement in FORTRAN is most
similar to the multi-alternative choice statement in Ada, Java and Pascal?
The computed goto
- 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.