ADA programming language
-
Upload
aysha-kalsoom -
Category
Education
-
view
877 -
download
2
description
Transcript of ADA programming language
04
/10
/20
23
1
ADA PROGRAMMING LANGUAGE
04
/10
/20
23
2
LIST OF CONTENTS..
ADA short introduction Data Types Sub-Types Derived-Types Strongly Typed Dynamic Typing Control Structures Case Statements Loops (Conditional)
04
/10
/20
23
3
ADA
Ada is a programming language which is suitable for all development needs, named after Augusta Ada King.
Ada has built-in features that directly support: Structured programming Object-oriented programming Generic programming Distributed programming Concurrent programming.
04
/10
/20
23
4
ADA FEATURES
Strongly Typed Packages can be defined. Packages and types can be made generic. Tasks can be created and communicate. Predefined library Object-oriented programming is included. Interfaces to other languages are included in
this languages.
04
/10
/20
23
5
DATA TYPES
A Type is given by a set of values and their operations. Ada allows us to define our own data types,
including numeric data types. The elementary Ada types are:
Scalar Types Discrete Types Real Types Fixed Point Types Access Types
04
/10
/20
23
6
SCALAR TYPES
The predefined package Standard contains declarations for the standard types such as integer, float, character and boolean, as well as defining the operations available on them.
The following operations are defined for all scalar types. =, /= Equality, inequality <, <=, >, >= in, not in Range membership text
04
/10
/20
23
7
INTEGER TYPES
The following are examples of integer declarations.
Here the standard predefined integer type is used. Count : Integer; X,Y,Z : Integer; Amount : Integer := 0; Unity : constant Integer := 1; A_Month : Integer range 1..12;
04
/10
/20
23
8
INTEGER TYPES
The following operators are also defined for all integer types. +,-,*,/ ** Exponentiation (integer exponent only) Mod Modulus rem Remainder Abs Absolute value
04
/10
/20
23
9
FLOATING POINT TYPES
The following are examples of floating point declarations.
Floating point numbers have a relative error. x : float; a,b,c : float; pi : constant float := 3.14; Avogadro : constant := 6.027E23;
04
/10
/20
23
10
FLOATING POINT TYPES
The following operators are also defined for all float types. +,*,/,- ** Exponentiation (integer exponent only) abs Absolute value
04
/10
/20
23
11
FIXED POINT TYPES
Fixed point numbers have a bounded error, the absolute value of which is called the delta of the type.
The following is example of fixed point declarations. type Volt is delta 0.125 range 0.0 .. 255.0;
04
/10
/20
23
12
ENUMERATION TYPES
An enumeration type is defined by listing all the possible values of the type. type Computer_Language is (Assembler, Cobol,
Lisp, Pascal, Ada); type C_Letter_Languages is (Cobol, C);
There are two predefined enumerated types in the package STANDARD: Type character. Type boolean.
04
/10
/20
23
13
TYPE CHARACTER
There are two built-in character types in Ada Simple 8-bit ASCII Characters Wide_Character that support 16-bit Unicode/ISO
standard 10646. We can define our own types just like the
integer types type LetterGrade is ( ‘A’, ‘B’, ‘C’, ‘D’, ‘F’, ‘I’, ‘W’);
04
/10
/20
23
14
TYPE BOOLEAN The two values of boolean variables is true and false. The following operators can be used with boolean types:
and or not xor /= = 'and then' 'or else'
Ada will not allow an unparenthesied expression to contain both and's and or's.
This decreases the likelihood of misreading the intentof a complicated boolean expression.
E.g. (a < b) and (b > c) or (d < e) -- illegal ((a < b) and (b > c)) or (d < e) -- ok
04
/10
/20
23
15
SUB-TYPES We can restrict the range of values a variable can take
by declaring a subtype with a restricted range of values (this corresponds to
Pascal's user defined types). Any attempt to place an out-of-range value into a
variable of a subtype results in an exception (program error).
In this way program errors can be discovered. The syntax for a subtype declaration is
subtype Name is Base_Type; subtype Name is Base_Type range lowerbound . . upperbound;
Examples of declaring subtypes are given below. type Processors is (M68000, i8086, i80386, M68030,
Pentium, PowerPC); subtype Old_Processors is Processors range M68000..i8086; subtype New_Processors is Processors range Pentium..PowerPC;
04
/10
/20
23
16
DERIVED-TYPES
When subtypes are created they are still compatible with their base type. Sometimes we may wish to create distinctly new types that are not associated with the original type at all.
To do this we create a derived type from a parent type using the following syntax
type Name is new Parent_Type; type Name is new Parent_Type range lower bound . .
upper bound;
04
/10
/20
23
17
DERIVED-TYPES
A derived type is a completely new type and is incompatible with any other type, even those derived from the same parent type.
Derived types should be used when the modeling of a particular object suggests that the parent type is inappropriate, or you wish to partition the objects into distinct and unmixable classes. type Employee_No is new Integer; type Account_No is new Integer range
0..999_999;
04
/10
/20
23
18
Ada Type
Composite TypesElementary Types
TaskArray RecordProtecte
dAccess Scalar
Discrete Real
IntegerEnumeration
ModularSigned OrdinaryDecimal
FixedFloat
04
/10
/20
23
19
STRONGLY TYPED A strongly-typed programming language is one in
which each type of data (such as integer, character, hexadecimal, packed decimal, and so forth) is predefined as part of the programming language and all constants or variables defined for a given program must be described with one of the data types.
Ada is a strongly typed programming language. A large number of compile-time checks are
supported to help avoid bugs that would not be detectable until run-time in some other languages or would require explicit checks to be added to the source code.
04
/10
/20
23
20
STRONGLY TYPED
For example, the syntax requires explicitly named closing of blocks to prevent errors due to mismatched end tokens.
The adherence to strong typing allows detection of many common software errors (wrong parameters, range violations, invalid references, mismatched types, etc.) either during compile-time, or otherwise during run-time.
Compilers also commonly check for misspelled identifiers, visibility of packages, redundant declarations, etc. and can provide warnings and useful suggestions on how to fix the error.
04
/10
/20
23
21
DYNAMIC TYPING
Dynamic Typing is the property of a language where type checks are performed mostly at run time.
Where values in Pascal or C must be static (e.g. the subscript bounds of an array) they may be dynamic in Ada.
However, static expressions are required in certain cases where dynamic evaluation would not permit a reasonable implementation (e.g. in setting the number of digits of precision of a floating point type).
04
/10
/20
23
22
Control Structures Conditionals
If Statements Case Statements
Loops
04
/10
/20
23
23
CONTROL STRUCTURES
The control structures of Ada are similar in style to most conventional languages. However some differences remain.
As usual Ada control structures are designed for maximum readability, all control structures are clearly ended with an 'end something'.
04
/10
/20
23
24
IF STATEMENTS
All if statements end with an end if statement. if condition then
statement;else
other statement;end if;
04
/10
/20
23
25
To prevent the common sight of if's marching across the page there is the elsif structure. As many elsifs as required can used. Note the spelling of elsif carefully. if condition then
statement; elsif condition then
other statement; elsif condition then
other statement;...
else condition thenanother statement;
end if; The final else is optional in this form of the if.
04
/10
/20
23
26
CASE STATEMENTS Often it is necessary to compare one specific variable
against several constant expressions. For this kind of conditional expression the case statement exists. For example:
case X iswhen 1 => Walk_The_Dog;when 5 =>
Launch_Nuke;when 8 | 10 =>
Sell_All_Stock;when others =>
Self_Destruct;end case;
04
/10
/20
23
27
LOOPS (CONDITIONAL)
Loops allow you to have a set of statements repeated over and over again. Endless Loop Loop with Condition at the Beginning loop with condition at the end loop with condition in the middle for loop
04
/10
/20
23
28
ENDLESS LOOP
as a relative term here — if the computer is switched off then even endless loops will end very abruptly. Endless_Loop :
loop Do_Something; end loop Endless_Loop;
The loop name (in this case, "Endless_Loop") is an optional feature of Ada.
Naming loops is nice forreadability but not strictly needed.
04
/10
/20
23
29
LOOP WITH CONDITION AT THE BEGINNING
This loop has a condition at the beginning. The statements are repeated as long as the condition is met.
If the condition is not met at the very beginning then the statements inside the loop are never executed. While_Loop :
while X <= 5 loopX := Calculate_Something;
end loop While_Loop;
04
/10
/20
23
30
LOOP WITH CONDITION AT THE END
This loop has a condition at the end and the statements are repeated until the condition is met.
Since the check is at the end the statements are at least executed once. Until_Loop :
loopX := Calculate_Something;exit Until_Loop when X > 5;
end loop Until_Loop;
04
/10
/20
23
31
LOOP WITH CONDITION IN THE MIDDLE Sometimes you need to first make a calculation and
exit the loop when a certain criterion is met. However when the criterion is not met there is
something else to be done. Hence you need a loop where the exit condition is in the middle. Exit_Loop :
loop X := Calculate_Something; exit Exit_Loop when X > 5; Do_Something (X);end loop Exit_Loop;
In Ada the exit condition can be combined with any other loop statement as well. We can also have more then one exit statement.
04
/10
/20
23
32
FOR LOOP Quite often one needs a loop where a specific variable is
counted from a given start value up or down to a specific end value. For_Loop :
for I in Integer range 1 .. 10 loop Do_Something (I)end loop For_Loop;
You don't have to declare both type and range as seen in the example.
If you leave out the type then the compiler will determine the type by context and leave out the range then the loop will iterate over every valid value for the type given.
As always with Ada: when "determine by context" gives two or more possible options then an error will be displayed and then you have to name the type to be used.
Ada will only do "guess-works" when it is safe to do so.
04
/10
/20
23
33
CONCLUSION
Ada is a huge language. It is to be remembered that the main objective of the Ada design was to incorporate the contemporary software engineering knowledge. Ada is used in the areas of Air Traffic Control Systems (in various countries), Railway Transportation Systems (such as Paris Metro and Hong Kong Subway) and Banking/Finance (Reuters and others).