Gem #145: Ada Quiz 3 - Statements

by Valentine Reboul —AdaCore

Let's get started...

Ada is an imperative programming language, where the sequentially executed statement is a building block of the language (together with declarations). This Gem presents nine short questions on Ada statements. Try to answer them without using the compiler.

Q1 - Is there a compilation error?

if A == 0 then
   Put_Line ("A is 0");
end if;

Q2 - Is there a compilation error?

if A := 0 then
   Put_Line ("A has been assigned the value zero");
end if;

Q3 - Is there a compilation error?

declare
   A : Integer := Integer'Value (Get_Line);
begin
   case A is
      when 1 .. 9 =>
         Put_Line ("Simple digit");
      when 10 .. Integer'Last =>
         Put_Line ("Long positive");
      when Integer'First .. -1 =>
         Put_Line ("Negative");
   end case;
end;

Q4 - Is there a compilation error?

declare
   A : Integer := Integer'Value (Get_Line);
begin
   case A is
      when Positive =>
         Put_Line ("Positive");
      when Natural =>
         Put_Line ("Natural");
      when others =>
         Put_Line ("Other");
  end case;
end;

Q5 - Is there a compilation error?

declare
   A : Float :=  10.0;
begin
  case A is
     when 1.0 .. Float'Last =>
        Put_Line ("Positive");
     when Float'First .. -1.0 =>
        Put_Line ("Negative");
     when others =>
        Put_Line ("others");
  end case;
end;

Q6 - Is there a compilation error?

for Index in 0 .. 10 loop
   Index := 10;
end loop;

Q7 - What is the output of this code?

Put_Line ('Before the loop');
for Index in 10 .. 0 loop
   Put_Line (Integer'Image (Index));
end loop;
Put_Line ('After the loop');

Q8 - Is there a compilation error?

if A != 0 then
   Put_Line ("A is not 0");
end if;

Q9 - What is the output of this code?

declare
   Index : Integer := 20;
begin
   for Index in 1 .. 5 loop
      Put_Line (Integer'Image(Index));
   end loop;
   Put_Line (Integer'Image(Index));
end;

Q10 - What is the output of this code?

declare
  X : Integer := 2;
begin
   for I in 1 .. X loop
      X := 10;
      Put_Line ("One loop iteration");
   end loop;
end;

Answers:

A1 - Compilation error: The Ada equality symbol is "=", not "==".

A2 - Compilation error: Assignment is not an operator in Ada. Therefore it can never be used in an expression or Boolean condition.

A3 - Compilation error: The covered intervals are Integer'first to -1, 1 to 9, and 10 to Integer'last. Obviously zero is missing. However, all values covered by the subtype of the case expression must be covered by the case statement alternatives. The compiler will complain about the missing value.

A4 - Compilation error: Positive and Natural are subtypes defined in the predefined package Standard as follows:

subtype Natural is Integer range 0 .. Integer'Last;

subtype Positive is Integer range 1 .. Integer'Last;

Their ranges overlap, so cannot be used together in a case statement, since each value covered by the choices in a case statement must occur only once.

A5 - Compilation error: Float is not a discrete type, so it cannot be used for the type of the expression of a case statement.

Summarizing the answers to questions 3, 4, and 5, in a case statement, each value belonging to the subtype of the expression, which must be a discrete, static subtype, must be covered once and only once by the case alternative.

A6 - Compilation error

In an Ada for loop, the loop_parameter is a constant: it cannot be updated within the sequence of statements of the loop.

A7 - The output is:

	Before the loop
	After the loop 

Nothing is printed during execution of the loop itself, because the range 10 .. 0 is empty, so the loop doesn't loop!

The correct way to get the 11 numbers printed from 10 to 0 is to use a reverse loop:

for Index in reverse 0 .. 10 loop
   Put_Line (Integer'Image (Index));
end loop;

A8 - Compilation error: The Ada inequality symbol is "/=", not "!=".

A9 - The output is:

  1
  2
  3
  4
  5
  20

It's unnecessary to declare Index before using it as a for loop index. The for loop effectively declares its own loop variable, which will hide any outer object with the same name.

A10 - The output is:

One loop iteration
One loop iteration

The range of the loop parameter is determined once, at the start of the loop. The modification of X after that point has no effect on the number of loop iterations.


About the Author

Valentine Reboul joined AdaCore in 2012 after 3 years of experience in critical systems (Air Traffic Flow Management and Railway automation solutions). She now participates in the "Qualifying Machine" research project and is involved in training sessions given about Ada Language. She holds an engineering degree from the Ecole Nationale Supérieure d'Informatique et de Mathématiques Appliquées (Grenoble, France).