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.
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
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;
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.
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;
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;
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
.
|
Introduction to Conditions
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment