Home

Introduction to Conditions

        

Logical Operators

 

Equality =

To compare two variables for equality, you use the = operator. The formula used is:

value1 = Value2

The equality operation is used to find out whether two variables (or one variable and a constant) hold the same value. From our syntax, the compiler would compare the value of Value1 with that of Value2. If Value1 and Value2 hold the same value, the comparison produces a true result. If they are different, the comparison renders false.

Here is an example:

with Ada.Text_IO;
use Ada.Text_IO;

procedure Exercise is
   value1 : Integer := 15;
   value2 : Integer := 24;
begin
   Put_Line("Value 1 = " & natural'image(value1));
   Put_Line("Value 2 = " & natural'image(value2));
   Put_Line("Comparison of value1 = 15 produces " & boolean'image(value1 = 15));
end Exercise;

This would produce:

Value 1 = 15
Value 2 = 24
Comparison of value1 = 15 produces TRUE

Inequality /=

As opposed to equality, to find out if two values are not equal, you use the /= logical operator. Its formula is:

Value1 /= Value2

The /= is a binary operator (like all logical operators) that is used to compare two values.

Less Than: <

To find out whether one value is lower than another, use the < operator. Its syntax is:

Value1 < Value2

The value held by Value1 is compared to that of Value2. As it would be done with other operations, the comparison can be made between two variables, as in Variable1 < Variable2. If the value held by Variable1 is lower than that of Variable2, the comparison produces a true or positive result.

Flowchart: Less Than

Less Than or Equal To: <=

The previous two operations can be combined to compare two values. This allows you to know if two values are the same or if the first is less than the second. The operator used is <= and its syntax is:

Value1 <= Value2

The <= operation performs a comparison as any of the last two. If both Value1 and VBalue2 hold the same value, result is true or positive. If the left operand, in this case Value1, holds a value lower than the second operand, in this case Value2, the result is still true.

Less Than Or Equal

Greater Than: >

To find out if one value is greater than another, use the > operator. Its formula is:

Value1 > Value2

Both operands, in this case Value1 and Value2, can be variables or the left operand can be a variable while the right operand is a constant. If the value on the left of the > operator is greater than the value on the right side or a constant, the comparison produces a true or positive value . Otherwise, the comparison renders false or null:

Greater Than

 

Greater Than Or Equal To: >=

The greater than or the equality operators can be combined to produce an operator as follows: >=. This is the "greater than or equal to" operator. Its formula is:

Value1 >= Value2

A comparison is performed on both operands: Value1 and Value2. If the value of Value1 and that of Value2 are the same, the comparison produces a true or positive value. If the value of the left operand is greater than that of the right operand,, the comparison produces true or positive also. If the value of the left operand is strictly less than the value of the right operand, the comparison produces a false or null result:

Flowchart: Greater Than Or Equal To

 

 

 
 
 

Boolean Variables

 

Introduction

A Boolean variable is a variable that can hold a value as being True or False. To declare a Boolean variable, use the Boolean keyword. Here is an example:

with Ada.Text_IO;
use Ada.Text_IO;

procedure Exercise is
   IsDrunk : Boolean;
 
begin

end Exercise;

By default, when a new Boolean variable has just been declared, it receives a value of False.

Showing the Value of a Boolean Variable

To display the value of a Boolean variable, type Boolean'image(). In the parentheses, enter the value of the variable. Here is an example:

with Ada.Text_IO;
use Ada.Text_IO;

procedure Exercise is
   IsDrunk : Boolean;
 
begin
   Put_Line("Driver is drunk: " & Boolean'image(IsDrunk));
end Exercise;

This would produce:

Driver is drunk: FALSE

Initializing a Boolean Variable

To specify the value of a Boolean variable, assign a value of True or false to it. Here is an example:

with Ada.Text_IO;
use Ada.Text_IO;

procedure Exercise is
   IsDrunk : Boolean := TRUE;
 
begin
   Put_Line("Driver is drunk: " & Boolean'image(IsDrunk));
end Exercise;

 

Enumerations

 

Introduction

An enumeration is a series of constant integers that each has a specific position in the list and can be recognized by a meaningful name. Based on this, instead of just remembering that the constant 1 represents Single Family, you can create a list that has that type of house. In another list, instead of using 1 again, you can give it a name. Consequently, in each list, although the constant 1 would still be considered, at least it would mean something precise.

Creating an Enumeration

To create an enumeration, you start with the type keyword, followed by the name of the enumeration, followed by is (). In the parenthese, type a name for each item of the list. The formula of creating an enumeration is:

type Enumeration_Name is (Item1, Item2, Item_n);

Here is an example of creating an enumeration:

with Ada.Text_IO;
use Ada.Text_IO;

procedure Exercise is
   type HouseType is (Unknown, SingleFamily, TownHouse, Condominium);

begin
   
end Exercise;

By default, the members of an enumeration are constant integers. If you want, you can create them as characters. To do this, create the list made of characters and include each member in single-quotes. Here is an example:

with Ada.Text_IO;
use Ada.Text_IO;

procedure Exercise is
   type HouseTypes is (Unknown, SingleFamily, TownHouse, Condominium);
   type Genders is ('m', 'f', 'u');
   
begin
   
end Exercise;

 

Declaring an Enumeration Variable

After creating an enumeration, you can declare a variable from it. Here is an example:

with Ada.Text_IO;
use Ada.Text_IO;

procedure Exercise is
   type HouseTypes is (Unknown, SingleFamily, TownHouse, Condominium);
   type Genders is ('m', 'f', 'u');
   
   HouseCategory : HouseTypes;
   
begin
   
end Exercise;

Initializing an Enumeration Variable

After declaring a variable for an enumeration, to initialize it, specify which member of the enumeration would be assigned to the variable. You should only assign a known member of the enumeration. Here is an example:

with Ada.Text_IO;
use Ada.Text_IO;

procedure Exercise is
   type HouseTypes is (Unknown, SingleFamily, TownHouse, Condominium);
   type Genders is ('m', 'f', 'u');
   
   HouseCategory : HouseTypes := SingleFamily;
   
begin
   
end Exercise;

You can also find out what value the declared variable is currently holding.

To display the value of the variable, use the name of the enumeration followed by 'image(). In the parentheses, enter the name of the variable. Here is an example:

with Ada.Text_IO;
use Ada.Text_IO;

procedure Exercise is
   type HouseTypes is (Unknown, SingleFamily, TownHouse, Condominium);
   type Genders is ('m', 'f', 'u');
   
   HouseCategory : HouseTypes := SingleFamily;
   
begin
   Put_Line("House Type: " & HouseTypes'image(HouseCategory));
  
end Exercise;

This would produce:

House Type: SINGLEFAMILY

.

 
 
   
 

Previous Copyright © 2010 FunctionX, Inc. Next