Ada Terminology

 

Ada.Text_IO.Put ( Item => “stop” );

   |            |        |        |               |

   |            |        |        |         actual parameter

   |            |        |    formal parameter

   |            |    procedure

   |       package

parent package

 

Global Variable – declared within the declarative region of the main program

Local Variable – declared within the declarative region of a procedure or function

Stub – dummy procedure or function that assists in testing part of a program

Driver – a simple main program used to call a procedure or function being tested

Discrete Data Type – each value has successor and predecessor (except first and last),

 such as integer, Boolean, character, enumerated types

Scalar Data Type – ordered and atomic, such as integer, character, float, Boolean

Atomic – only a single value to be associated with an identifier of that type

 

 

Basic Ada Syntax

 

Ada Reserved Words

abort                else                  new                  return

abs                   elsif                  not                   reverse

abstract            end                   null                  

accept              entry                                         select

access              exception                                  separate

aliased              exit                   of                     subtype

all                                             or                    

and                   for                    others               tagged

array                function            out                   task

at                                                                     terminate

                        generic             package           then

begin                goto                 pragma             type

body                                        private             

                        if                      procedure

case                 in                      protected          until

constant            is                                              use

                                                raise

declare                                     range                when

delay                limited              record              while

delta                 loop                 rem                  with

digits                                        renames

do                    mod                 requeue            xor

 

 

Declarative style:

1) Named Numbers

2) Programmer-Defined Types and Subtypes

3) Constants of Programmer-Designed Types and Subtypes

4) Instantiations of Generic Packages

5) Procedures and Functions

6) Variables

 

 

Declaring variables and constants:

X : Integer;

Y : Float;

Output : String(1..30);

Char : Character;

Num : Integer range 1..100;

Pi : constant float is 3.14139;

Message : constant string is “Stop”;

Question : Boolean;

 

 

Operators:

+, -, *, /

Rem     (remainder) (10 Rem 3 = 1)

Abs      (absolute value) (Abs(-3) = 3)

_**_    (power) (3**2 = 9) (3**-2 = error) (2.0**-2 = 0.25)

&         (concatenation) (‘A’ & “BC” = “ABC”)

 

 

Order of precedence:

1) ( )

2) **, Abs

3) *, /, Rem

4) Unary +, Unary -

5) +, -, &

 

 

Relation operators:

=,  <,  >,  <=,  >=

/=         **not equal

 

 

Packages:

Ada.Text_IO

Ada.Integer_Text_IO;

Ada.Float_Text_IO;

Ada.Numerics; **contains Pi and e

Ada.Numerics.Elementary_Functions;  

**contains Sqrt, Log, Exp, and trigonometric functions

Ada.IO_Exceptions;

 

 

Basic Procedures:

Put                                           **output data  

Get                                           **input data

Get_Line                                  **input a string of data

New_Line                                **output goes to a new line

New_Line (Spacing => X)        **output skips X amount of lines

Skip_Line                                 **input is read from a new line (Spacing is used here also)

 

 

Type convergence:

Integer(X)                     **if X is float it will round to the number furthest from 0

            Ex. Integer(4.5) = 5

            Ex. Integer(-2.5) = -3

Float(X)                       **if X is an integer it will be changed into its equal in float

 

 

Outputting numerical data:

Integer

Put (Item => 345);

            Output = “       345”                 **default width is 10

Put (Item => 345, Width => 5);

            Output = “  345”

Put (Item => 345, Width => 0);

            Output = “345”

 

Float

Put (Item => 3.27, Fore => 10, Aft => 1, Exp => 0)

            Output = “         3.3”

 

 

Inputting an outputting string data:

Get_Line (Item => Mes, Last => Lt);              

**Lt must previously be defined as an integer

Put (Item => Mes (1..Lt));

 

Rules of string i/o

Ex. Mes : String(1..6);

      Mes := “ABCDEF”;

 

            Mes(1..2) = “AB”

            Mes(1) = ‘A’

            Mes(5..4) = null string

            Mes(0..5) = constraint error

 

String comparisons

            ** Two strings can be compared using relation operators

Ex. if (str1 = str2) then

Ex. while CurrentName < EmpName loop

 

 

Working with Boolean variables:

To set

Found := True;

Found := False;

 

Operators

And, Or, Not                                       **May reference relational operators

 

            Demorgan’s Law

                        Not (A And B) = Not A Or Not B

                        Not (A Or B) = Not A And Not B      

 

 

Discrete and scalar data type attributes:

(Scalar & Discrete)

First                             Ex. Integer’First

Last                              Ex. Integer’Last

Image               **returns a string in uppercase

                                    Ex. Color_type’Image(red) = “RED”

                                    Ex. Integer’Image(17) = “ 17”

Value                           Ex. Integer’Value(“ 17”) = 17

 

(Discrete Only)

Succ                             Ex. Integer’Succ(18) = 19

Pred                             Ex. Integer’Pred(18) = 17

Pos                              Ex. Color_Type’Pos(red) = 0

                                    Ex. Integer’Pos(20) = 20

Val                               Ex. Color_Type’Val(0) = “RED”

                                    Ex. Character’Val(07) = bell sound

 

 

Data Files

 

1) Declare file variable:

In_emp_file : Ada.Text_IO.File_Type;

 

 

2) Prepare file for I/O:

Ada.Text_IO.OPEN (File => In_emp_file, Mode => Ada.Text_IO.In_file, Name => “emp.txt”);

            ** OPEN is for an exiting file, and CREATE is to produce a new file

            ** In_file - to get input from a file, Out_file – to write to a file,

Append_file - to write to a file without overwriting preexisting data.

 

 

3) Specifying files in text_IO procedures:

Ada.Text_IO.get (File => In_emp_file, Item => Name);

 

 

4) Close the file:

Ada.Text_IO.Close (File => In_emp_file);

 

 

Useful Procedures

 

End_of_File                              **for reading files, to check if file is finished

            If not Ada.Text_IO.End_of_File (emp_file) then

 

End_of_Line                             **to assure you do not try to read passed the line terminator

            Exit emp_loop when Ada.Text_IO.End_of_Line (emp_file);

 

Put Line                                    **writes a string to the screen or file then skips a line

            Ada.Text_IO.Put_Line (“Ada ROCKS!!!”);

 

Delete                                      **to delete a file

            Ada.Text_IO.Delete (File => emp_file);

 

Is_Open                                   **to check if file is already open

            If Ada.Text_IO.Is_Open (File => emp_file) then

                        ~~~~~~

            else --open the file--

 

New_Page                               **to go to a new page

            Ada.Text_IO.New_Page (File => emp_file);

 

New_Line                                **end current line and leave any amount of blank lines

            Ada.Text_IO.New_Line (Spacing => 4);

 

Name                                       **outputs the name of the file

            Ada.Text_IO.Put (Item => Ada.Text_IO.Name (emp_file));

                        **the output will be emp.txt

 

Set_Col                                    **shifts cursor

            Ada.Text_IO.Set_Col (to => 10);

 

 

Enumerated Data Types

 

1) Type Flag_Color_Type is (Red, White, Blue);

            ** Red has value of 0, White has 1, and Blue has 2

 

2) Package color_IO is New Ada.Text_IO.Enumeration_IO (Enum=>Flag_Color_Type)

 

3) Color : Flag_Color_Type;

 

4) color_IO.get(Item=>Color);

 

 

Subtypes

 

Subtype Name_Type is string (1..20);

Subtype Age_Type is integer range 1..100;

 

Name : Name_Type;

yrs : Age_Type;

 

 

Predefined subtypes:

Natural                         **0 - Integer’Last

Positive                        **1 – Integer’Last

 

 

Loops

 

General Loop:

Loop                                                                <= exit after “Loop” is a pretest loop

            ~~~~~~

            exit when ( X > 10 );                            <= exit in middle is a midtest loop

            ~~~~~~

end loop;                                                          <= exit before “end loop” is a posttest loop

 

 

For Loop:

For i in 1..10 loop                                                        **For i in reverse 1..10 loop

            ~~~~~~

end loop;

 

 

While Loop:

While total < 100 loop

            ~~~~~~

end loop;

 

 

Procedures

 

Procedure Math (num1 : in integer, num2 : out integer) is        

            (Declarative section for new variables)

begin

            ~~~~~~

end;                                                                  ** in out to pass a variable both ways

 

 

To call a procedure with positional association:

Math (x , y);    or    Math (num1 , num2);

 

 

To call a procedure with named association:

Math (num1 => x , num2 => y);

                                                                        **num1, num2 are formal parameters

                                                                        **x, y are actual parameters

 

 

Functions

 

Function Tax (x , y : in integer) return float is

            Total : float;

begin

            ~~~~~~

            return Total;

end Tax;

 

 

To call a function:

Price := Tax (A , B);

 

 

Case Statements

 

Case X is

            when 7..10 =>

                        ~~~~~~

            when 11|12 =>

                        ~~~~~~

            when others =>

                        null;

end case;

 

 

Records

 

Type student_rec is

            Record

                        Name : name_str;                    **name_str and state_str are subtypes

                        State : state_str;

                        Credits : integer;

            end record;

 

 

Declare your variable:

Stu : student_rec;

 

 

Ways to assign:

Stu.credits := 12;

Stu := Stu2;

Stu := (“Sam_Martin”, “PA”, 15);

 

 

To use hierarchical records:

Employee.Personal.Sex := ‘F’;

                                    **Employee is a record containing a record called Personal, Personal then contains a variable entitled Sex

 

 

Exception Handling

 

To declare use of exception handling:

with Ada.IO_Exceptions;

 

 

To use:

~~~~~~

Input_Block:

begin

            ~~~~~~

            exception

                        when Constraint_Error | Ada.IO_Exceptions.Data_Error =>

                                    ~~~~~~

                        when Ada.IO_Exceptions.Device_Error =>

                                    ~~~~~~

            ~~~~~~

end Input Block;

~~~~~~

 

 

The exceptions:

Constraint_Error          

**raised when a variable is assigned a value that is not within its range

 

Program_Error

**raised when an attempt to execute an action is erroneous, such as reaching the end of a function with no return statement

 

Storage_Error

            **raised when a program requires more memory than is available

 

Tasking_Error

            **raised during intertask communication

 

Ada.IO_Exceptions.Data_Error

**raised by the get procedure if the type inputted is not of the correct type or if it is out of range

 

Ada.IO_Exceptions.Device_Error

            **raised if an I/O operation cannot be completed because of a malfunction

 

Ada.IO_Exceptions.End_Error

            **raised by an attempt to read or skip past the end of a file

 

Ada.IO_Exceptions.Layout_Error

**raised by an attempt in text input-output to set the column or line numbers in excess of specified maximum line or page length

 

Ada.IO_Exceptions.Mode_Error

**raised by attempting to use I/O operations with a file whose current mode is not compatible with the operation

 

Ada.IO_Exceptions.Name_Error

**raised by a call to Open if the string given for the parameter Name does not uniquely identify an external file

 

Ada.IO_Exceptions.Status_Error

**raised by an attempt to operate on a file that has not been prepared with a call to Open or Create

 

Ada.IO_Exceptions.Use_Error

**raised by an attempt to perform an impossible operation on an external file, such as a call to Open that attempts to open a file for input to a printer

 

 

Arrays

 

Initialize an array

Type Num_Array is array (1..20) of Integer;               

**a subtype can be made to replace 1..20

Type Color_Array is array (Color_Type) of Integer;    

**holds integers, with index of color_type

 

Initialize a variable of that array type

            X : Num_Array;

            Y : Num_Array;                       **X := Y is valid

 

 

Array Aggregates:
           
assume score is an array of integers

Ex. Score := (12, 24, 31, 15);

Ex. Score := (1 => 12, 2 => 24, 3 => 31, 4 => 15);

Ex. Score := (1 => 10, others => 0);

Ex. Score := (others => 0);

Ex. Score := (Num_Array’(others => 0);

Ex. Score := (1..5 => 0);

Ex. Score := (index_range => 0);

 

 

Array of records:

1) Declare a record. (stu_rec)

 

2) Declare the array of records.

Type Stu_Array is array (1..100) of Stu_Rec;

 

3) Declare variables of the array type.

            Class : Stu_Array;

4) Use the array

 

            Ex. Class(1).name := “John”;

            Ex. Class(34).age := 19;

 

 

Unconstrained arrays:

Declare the unconstrained array

Type Score_Array is array (integer range <>) of Integer;

 

Declare subtypes of the array with bounds

Subtype Class1_Array is Score_Array(1..50);

 

Declare variables

Class1 : Class_Array;

 

 

Two dimensional arrays:

Create subtypes for the rows and columns

Subtype row_index is integer range 1..4;

Subtype column_index is integer range 1..7;

 

Declare the 2 dimensional array

            Type Matrix is array (row_index, column_index) of integer;

 

Declare variables

            Spending : Matrix;

 

To use the 2 dimensional array

            For i in row_index loop

                        For j in column_index loop

                                    Get (Item => Spending (i, j));

                        end loop;

            end loop;

 

            Put (Item => Spending (2, 4));             **output data in row 2, column 4

 

Initialize the array

            Spending := (others => (others => 0));

 

Hosted by www.Geocities.ws

1