3.4 if Statements

[ Table of Contents ] Prev ] Chapter Overview ] Next ] [ Glossary/Index ]

If statements are used to build "conditional" or "branching" algorithms. They are compound statements, always using multiple lines of code and always using, as a minimum, the reserved words or combinations: if, then, and end if. Every if statement includes at least one Boolean expression, which evaluates to True or False.

General Form

if boolean_expression_1 then
      -- statement_sequence_1;
elsif boolean_expression_2 then
      -- statement_sequence_2;
elsif boolean_expression_3 then
      -- statement_sequence_3;
-- etc
else
      -- statement_sequence_N;
end if;

The elsif and else parts are optional, but for each one there must be at least one line of executable code. However, that line can be a null statement, which does nothing.

This diagram depicts a program containing five examples of  if statements. Two packages from the predefined environment are employed.

Im3-5.gif (4987 bytes)

Note that the "nested if statement" and the "if with logical operators" provide two ways of doing the same thing. The latter is easier to read. (See also the case statement version in the next section.)

Source Code Listing

------------------------------------------------------------------
--  This procedure displays messages about the current date, such 
--  as which half-year, quarter-year, or season we are in. 
------------------------------------------------------------------
with Ada.Text_IO, Ada.Calendar;
use  Ada.Text_IO;
procedure Show_Time_Of_Year is
                                              -- declarative part
  The_Time  : Ada.Calendar.Time;
  The_Month : Ada.Calendar.Month_Number;
  The_Day   : Ada.Calendar.Day_Number;

  type Month_Abr is (Jan, Feb, Mar, Apr, May, Jun,
                     Jul, Aug, Sep, Oct, Nov, Dec);
  Month_Abbreviation : Month_Abr;

begin                                         -- executable part

  The_Time  := Ada.Calendar.Clock;
  The_Month := Ada.Calendar.Month(The_Time);
  The_Day   := Ada.Calendar.Day(The_Time);

  Month_Abbreviation := Month_Abr'Val(The_Month);

  if The_Month < 7 then                       -- simple if statement
    Put_Line("We are in the first half year.");
  end if;

  if The_Month < 7 then                       -- if-else statement
    Put_Line("We are in the first half.");
  else
    Put_Line("We are in the second half.");
  end if;

  if The_Month < 4 then                       -- if-elsif-else statement
    Put_Line("We are in the first quarter.");
  elsif The_Month < 7 then
    Put_Line("We are in the second quarter.");
  elsif The_Month < 10 then
    Put_Line("We are in the third quarter.");
  else
    Put_Line("We are in the fourth quarter.");
  end if;

  if The_Month < 3 then                       -- nested if statements
    Put_Line("The season is Winter.");
  elsif The_Month = 3 then
    if The_Day < 21 then 
      Put_Line("The season is Winter.");
    else
      Put_Line("The season is Spring.");
    end if;
  elsif The_Month < 6 then
    Put_Line("The season is Spring.");
  elsif The_Month = 6 then
    if The_Day < 21 then
      Put_Line("The season is Spring.");
    else
      Put_Line("The season is Summer.");
    end if;
  elsif The_Month < 9 then
    Put_Line("The season is Summer."); 
  elsif The_Month = 9 then
    if The_Day < 21 then
      Put_Line("The season is Summer.");
    else
      Put_Line("The season is Fall.");
    end if;
  elsif The_Month < 12 then
    Put_Line("The season is Fall.");
  elsif The_Month = 12 then
    if The_Day < 21 then
      Put_Line("The season is Fall.");
    else
      Put_Line("The season is Winter.");
    end if;
  end if;

  if (The_Month = 12 and The_Day >= 21) or  -- if with logical operators
     (The_Month = 1) or (The_Month = 2) or
     (The_Month = 3  and The_Day < 21) then
    Put_Line("The season is Winter.");
     
  elsif (The_Month = 3 and The_Day >= 21) or
        (The_Month = 4) or (The_Month = 5) or
        (The_Month = 6 and The_Day < 21) then
    Put_Line("The season is Spring.");

  elsif (The_Month = 6 and The_Day >= 21) or
        (The_Month = 7) or (The_Month = 8) or
        (The_Month = 9 and The_Day < 21) then
    Put_Line("The season is Summer.");

  else 
    Put_Line("The season is Fall.");
  end if;

end Show_Time_Of_Year;
------------------------------------------------------------------

An alternative (more readable) way of coding the if-elsif-else example is to use a case statement, as illustrated in the next section. (That is why we included the Month_Abr type and Month_Abreviation variable.)

If the above program is run in July, it produces the following output:

  We are in the second half.
  We are in the third quarter.
  The season is summer.
  The season is summer.

Related Topics

3.5 case Statements 3.11 The null Statement
A.2 Simple and Compound Statements

[ Back to top of page Prev ] Next ]