Concatenating Strings
|
One of the most regular operations you can perform with
strings consists of adding one to another. This is referred to as concatenating.
To concatenate two strings, you use the & operator. Here is
an example:
with Ada.Text_IO;
use Ada.Text_IO;
procedure Exercise is
begin
Put_Line("Gertrude" & "Monay");
end Exercise;
This would produce:
GertrudeMonay
In the same way, you can concatenate as many strings as you
want. Here are examples:
with Ada.Text_IO;
use Ada.Text_IO;
procedure Exercise is
begin
Put_Line("Full Name: " & " Gertrude" & " " & "Monay");
end Exercise;
In the same way, you concatenate string variables. Here is
an example:
with Ada.Text_IO;
use Ada.Text_IO;
procedure Exercise is
FirstName : String := "Gertrude ";
LastName : String := "Monay";
begin
Put_Line(FirstName & LastName);
end Exercise;
You can also concatenate constant strings and string
variables. Here is an example:
with Ada.Text_IO; use Ada.Text_IO; procedure Exercise is FirstName : String := "Gertrude"; LastName : String := "Monay"; begin Put_Line("Full Name: " & FirstName & " " & LastName); end Exercise;
Integers
|
An integer is a numeric value for a natural number. In Ada,
an integral value is represented with the integer data type.
with Ada.Text_IO;
use Ada.Text_IO;
procedure Exercise is
number : integer;
begin
end Exercise;
Initialize an integer variable with a numeric value without
special characters. Here is an example:
with Ada.Text_IO;
use Ada.Text_IO;
procedure Exercise is
number : integer := 214685;
begin
end Exercise;
To display the value of the variable, in the parentheses of
Put_Line(), type natural'image() and, in the parentheses, type the name of the
variable. Here is an example:
with Ada.Text_IO;
use Ada.Text_IO;
procedure Exercise is
number : integer := 214685;
begin
Put_Line("Number: " & natural'image(number));
end Exercise;
This would produce:
Number: 214685
A natural number can include an underscore but would have
the same value as if the underscore did not exist. Here is an example:
with Ada.Text_IO;
use Ada.Text_IO;
procedure Exercise is
number : integer := 2146_85;
begin
Put_Line("Number: " & natural'image(number));
end Exercise;
You can also write a natural number in scientific format. To
do this, use either e or E. Here is an example:
with Ada.Text_IO;
use Ada.Text_IO;
procedure Welcome is
value1 : integer := 1558_2;
value2 : integer := 2241;
value3 : integer := 605E5;
begin
Put_Line("Value 1 = " & integer'image(value1));
Put_Line("Value 2 = " & integer'image(value2));
Put_Line("Value 3 = " & integer'image(value3));
end Welcome;
This would produce:
Value 1 = 15582 Value 2 = 2241 Value 3 = 60500000
Floating Point Numers
|
Introduction
|
A floating-point number is one that has a decimal separator.
In US English, the decimal separator is the period. Examples of floating-point
numbers are 12.58 or 27597.0249.
To declare a floating-point variable, use the float keyword.
To initialize it, assign a number that includes one decimal separator. Here is
an example:
with Ada.Text_IO; use Ada.Text_IO; procedure Welcome is value : float := 22.41; begin Put_Line("Value = " & float'image(value)); end Welcome;
A floating-point number can also include an underline. Here
is an example:
with Ada.Text_IO;
use Ada.Text_IO;
procedure Welcome is
value1 : float := 15.58_2;
value2 : float := 22.41;
begin
Put_Line("Value 1 = " & float'image(value1));
Put_Line("Value 2 = " & float'image(value2));
end Welcome;
A floating-pointing number can also be written in scientific
format. To represent it, use e or E. Here is an example:
with Ada.Text_IO; use Ada.Text_IO; procedure Welcome is value1 : float := 15.58_2; value2 : float := 22.41; value3 : float := 6.05E5; begin Put_Line("Value 1 = " & float'image(value1)); Put_Line("Value 2 = " & float'image(value2)); Put_Line("Value 3 = " & float'image(value3)); end Welcome;
This would produce:
Value 1 = 1.55820E+01 Value 2 = 2.24100E+01 Value 3 = 6.05000E+05
Here is an example that uses a declare section:
Here is an example:
with Ada.Text_IO; use Ada.Text_IO; procedure Exercise is begin declare number : Float; begin number := 248.36; put_line("Number = " & Float'Image(number)); end; end Exercise;
Specifying the Precision
|
with Ada.Text_IO, Ada.Float_Text_IO;
use Ada.Text_IO, Ada.Float_Text_IO; --, Example1;
procedure Exercise is
type Natural_Number is Range 1 .. 100;
number : Natural_Number;
type Decimal_Number is digits 2;
Single : Decimal_Number;
DecNbr : Float;
begin
number := 48;
Single := 4.8;
DecNbr := 15.528462;
Put("Number = ");
Put(Natural_Number'Image(number)); New_Line;
Put("Decimal = ");
Put(Decimal_Number'Image(Single)); New_Line;
Put("Decimal = ");
Put(DecNbr, Fore => 4, Aft => 2, Exp => 0); New_Line;
Put("Decimal = ");
Put(DecNbr, Fore => 4, Aft => 2, Exp => 3); New_Line;
end Exercise;
This would produce:
Number = 48 Decimal = 4.8E+00 Decimal = 15.53 Decimal = 1.55E+01
Constants
|
A constant is a value that doesn't change. To create a
constant, you use the constant keyword as follows:
VariableName : constant DataType := Value;
Here is an example:
with Ada.Text_IO;
use Ada.Text_IO;
procedure Welcome is
value1, value2 : integer;
value3 : constant integer := 605;
begin
value1 := 15582;
value2 := 2241;
Put_Line("Value 1 = " & integer'image(value1));
Put_Line("Value 2 = " & integer'image(value2));
Put_Line("Value 3 = " & integer'image(value3));
end Welcome;
Any type of variable can be created as a constant, as long
as you assign a value to it when declaring it. Here are example:
with Ada.Text_IO; use Ada.Text_IO; procedure Welcome is EmployeeNumber : constant integer := 202757; EmployeeName : constant String := "Herbert Solens"; begin Put_Line("Employee #: " & integer'image(EmployeeNumber)); Put_Line("Full Name: " & EmployeeName); end Welcome;
Once a constant has been created, you cannot assign a
new value to its variable. For example, the following will produce an error:
with Ada.Text_IO; use Ada.Text_IO; procedure Welcome is value1, value2 : integer; value3 : constant integer := 605; begin value1 := 15582; value2 := 2241; value3 := 39823; Put_Line("Value 1 = " & integer'image(value1)); Put_Line("Value 2 = " & integer'image(value2)); Put_Line("Value 3 = " & integer'image(value3)); end Welcome;
When you are creating a constant, if its value is a
number, you don't have to specify its data type. Here is an example:
with Ada.Text_IO; use Ada.Text_IO; procedure Welcome is EmployeeNumber : constant := 602058; begin Put_Line("Employee #: " & integer'image(EmployeeNumber)); end Welcome;
No comments:
Post a Comment