Tutorial

Basic Integer Comparison

When comparison between integers is being checked, relational operators will be used.  Relational operators are listed in Table 1.
 
 

Equality test
=
Non Equality test
/=
Less Than
<
Less Than or Equal
<=
Greater Than
>
Greater Than or Equal
>=
Table 1  Relational Operators

Comparison of real numbers requires a little caution in programming.  Standard comparisons such as         'A = B'  or  'C = 3.14' may not work properly in a program.  The real number may not be stored in memory exactly the way it was entered.  This is computer dependent and may cause problems when a program is executed on a different machine.  Figure 1 shows an example of a simple program, which was tested on a GNAT compiler, that compares two integers which a user enters.
 
 

with Ada.Integer_Text_IO, Ada.Text_IO;
use Ada.Integer_Text_IO, Ada.Text_IO;
procedure Integer_Compare is
   First_In, Second_In : Integer;
   Test : Character := 'C';
begin
while Test = 'C' or Test = 'c' loop -- Input comparison to see if user wants to remain in loop

   New_Line; New_Line; -- 2 carriage returns
   Put ("Enter a whole number:   "); -- output to screen
   Get (First_In); -- input from keyboard
   Put ("Enter another whole number:   ");
   Get (Second_In);

   if First_In > Second_In then -- Greater than comparison
      Put (First_In); Put (" is larger than"); Put (Second_In);
   elsif Second_In > First_in then -- Less than comparison
      Put (Second_In); Put (" is larger than"); Put (First_In);
   else  -- 
      Put ("Both numbers are the same");
   end if;
  
   New_Line; New_Line; -- Note two operations can be on one line when separated by a semi-colon
   Put ("Enter 'E' to exit or 'C' to continue:    ");
   Get (Test); 
   New_Line; New_Line;
end loop;
end Integer_Compare;

Figure 1   Integer comparison code example

When this program is run it would look like

Enter a whole number:   5
Enter another whole number:   23
23  is larger than       5
 

Enter 'E' to exit or 'C' to continue:    E
 

Go to the Tutorial Table of Contents