Computer Science Canada

[Ada-tut] Control Structures: If ... Else

Author:  wtd [ Mon Oct 11, 2004 5:36 pm ]
Post subject:  [Ada-tut] Control Structures: If ... Else

Background

In many programs there will be a time when you need to make a choice and do one of two or more possibilities based on the values of the variables at that point in the program.

Generally we call this a "conditional", and the usual words associated with it are "if" and "else". Ada is no different, except that we can have more than just two choices, and for the "in between" choices we use "elsif".

A simple example

So, let's look at a simple example. In thise example we'll compare 8 to 8. If they're the same, then all is well with the universe. If they're not, then the laws of mathematics have gone out the window.

code:
with Ada.Text_IO; use Ada.Text_IO;
procedure Simple_Program is
begin
   if 8 = 8 then
      Put_Line("All is well with the universe.");
   else
      Put_Line("The laws of mathematics went out the proverbial window.");
   end if;
end Simple_Program;


Another choice

If 8 equals 7, then the universe is almost right.

code:
with Ada.Text_IO; use Ada.Text_IO;
procedure Simple_Program is
begin
   if 8 = 8 then
      Put_Line("All is well with the universe.");
   elsif 8 = 7 then
      Put_Line("Close enough for government work.");
   else
      Put_Line("The laws of mathematics went out the proverbial window.");
   end if;
end Simple_Program;


You can include as many "elsif" conditions as necessary.


: