Chapter 3 The General Structure of Ada Programs

  • Slides: 31
Download presentation
Chapter 3 The General Structure of Ada Programs

Chapter 3 The General Structure of Ada Programs

General Form of an Ada Program With package 1; With package 2; . .

General Form of an Ada Program With package 1; With package 2; . . . With packagen; Procedure pname IS - - comments declaration of variables, constants, etc. . . Begin Program statement; . . . Program statement; End;

With clause b To inform the compiler for a package used in the program

With clause b To inform the compiler for a package used in the program b at least one “with” clause in Ada program b package for Text Input and Output • WITH Ada. Text_IO; b package for Integer Text Input and Output • WITH Ada. Integer_Text_IO; b package for Floating Text Input and Output • WITH Ada. Float_Text_IO;

Identifiers or names b Begin with a letter b Consist of only letters, digits,

Identifiers or names b Begin with a letter b Consist of only letters, digits, and underscores b Cannot use two or more underscore characters in succession b Cannot use an Ada reserved word or pre-defined identifier as an identifier b Pick meaningful names for identifiers b no restriction on the length of an identifier

Type of data b Character b Integer b Float

Type of data b Character b Integer b Float

Example-1 Displaying Initials Program WITH Ada. Text_IO; PROCEDURE Hello_Initials IS -------------------------------| Requests, then displays,

Example-1 Displaying Initials Program WITH Ada. Text_IO; PROCEDURE Hello_Initials IS -------------------------------| Requests, then displays, user's first and last initials. --| ------------------------------Initial 1 : Character; -- objects that hold initials Initial 2 : Character;

Cont. of example-1 BEGIN -- Hello_Initials -- Prompt for (request user to enter) user's

Cont. of example-1 BEGIN -- Hello_Initials -- Prompt for (request user to enter) user's initials Ada. Text_IO. Put(Item => "Enter your two initials> "); Ada. Text_IO. Get(Item => Initial 1); Ada. Text_IO. Get(Item => Initial 2); -- Display user's initials, with a greeting Ada. Text_IO. Put(Item => "Hello "); Ada. Text_IO. Put(Item => Initial 1); Ada. Text_IO. Put(Item => Initial 2); Ada. Text_IO. Put(Item => ". Enjoy studying Ada!"); Ada. Text_IO. New_Line; END Hello_Initials;

Reserve Words and Identifiers b Reserve Words With Procedure IS Begin END

Reserve Words and Identifiers b Reserve Words With Procedure IS Begin END

b Predefined Identifiers Ada. Text_IO Put New_Line Character Get

b Predefined Identifiers Ada. Text_IO Put New_Line Character Get

Input and Output b package for Text Input and Output • WITH Ada. Text_IO;

Input and Output b package for Text Input and Output • WITH Ada. Text_IO; b package for Integer Text Input and Output • WITH Ada. Integer_Text_IO; b package for Floating Text Input and Output • WITH Ada. Float_Text_IO;

Read Float Number b To read a float number into identifier “Inches” WITH Ada.

Read Float Number b To read a float number into identifier “Inches” WITH Ada. Float_Text_IO; Inches : Float; Ada. Float_Text. _IO. Get ( Item => Inches);

Read Integer Number b To read an integer number into identifier “Age” WITH Ada.

Read Integer Number b To read an integer number into identifier “Age” WITH Ada. Integer_Text_IO; Age : Integer; Ada. Integer_Text. _IO. Get ( Item => Age);

Read Character b To read a character into identifier “Initial 1” WITH Ada. Text_IO;

Read Character b To read a character into identifier “Initial 1” WITH Ada. Text_IO; Initial 1 : Character; Ada. Text. _IO. Get ( Item => Initial 1);

Write Float Number b To write a float number into identifier “Inches” WITH Ada.

Write Float Number b To write a float number into identifier “Inches” WITH Ada. Float_Text_IO; Inches : Float; Ada. Float_Text. _IO. Put ( Item => Inches);

Integer and Character output WITH Ada. Integer_Text_IO; Age : Integer; Ada. Integer_Text. _IO. Put

Integer and Character output WITH Ada. Integer_Text_IO; Age : Integer; Ada. Integer_Text. _IO. Put ( Item => Age); WITH Ada. Text_IO; Initial 1 : Character; Ada. Text. _IO. Put ( Item => Initial 1);

Example-2 Displaying the User’s Name WITH Ada. Text_IO; PROCEDURE Hello_Name IS -------------------------------------| Requests, then

Example-2 Displaying the User’s Name WITH Ada. Text_IO; PROCEDURE Hello_Name IS -------------------------------------| Requests, then displays, user's name --| ------------------------------------First. Name: String(1. . 10); -- object to hold user's name

Cont. of example 2 BEGIN -- Hello_Name -- Prompt for (request user to enter)

Cont. of example 2 BEGIN -- Hello_Name -- Prompt for (request user to enter) user's name Ada. Text_IO. Put (Item => "Enter your first name, exactly 10 letters. "); Ada. Text_IO. New_Line; Ada. Text_IO. Put (Item => "Add spaces at the end if it's shorter. > "); Ada. Text_IO. Get(Item => First. Name); -- Display the entered name, with a greeting Ada. Text_IO. Put(Item => "Hello "); Ada. Text_IO. Put(Item => First. Name); Ada. Text_IO. Put(Item => ". Enjoy studying Ada!"); Ada. Text_IO. New_Line; END Hello_Name;

Constant Object Declaration Form some_constant : CONSTANT type : = value; Example Pi :

Constant Object Declaration Form some_constant : CONSTANT type : = value; Example Pi : CONSTANT Float : = 3. 14159;

Assignment Statement Result_Variable : = expression; Example X : = Y + Z +

Assignment Statement Result_Variable : = expression; Example X : = Y + Z + 2. 0; X : = X +1;

Example-3 Converting Inches to Centimeters WITH Ada. Text_IO; WITH Ada. Float_Text_IO; PROCEDURE Inch_to_CM IS

Example-3 Converting Inches to Centimeters WITH Ada. Text_IO; WITH Ada. Float_Text_IO; PROCEDURE Inch_to_CM IS -------------------------------------| Converts inches to centimeters ------------------------------------CMPer. Inch : CONSTANT Float : = 2. 54; Inches : Float; Centimeters : Float;

Cont. of example-3 BEGIN -- Inch_to_CM -- Prompt user for value in inches Ada.

Cont. of example-3 BEGIN -- Inch_to_CM -- Prompt user for value in inches Ada. Text_IO. Put (Item => "Enter a length in inches> "); Ada. Float_Text_IO. Get (Item => Inches); -- Compute equivalent value in centimeters Centimeters : = CMPer. Inch * Inches; -- Display result Ada. Text_IO. Put (Item => "That equals "); Ada. Float_Text_IO. Put (Item => Centimeters); Ada. Text_IO. Put (Item => " centimeters"); Ada. Text_IO. New_Line; END Inch_to_CM;

New Line Procedure Form Ada. Text_IO. New_Line ( Spacing => positive number); Example Ada.

New Line Procedure Form Ada. Text_IO. New_Line ( Spacing => positive number); Example Ada. Text_IO. New_Line ( Spacing =>3);

Put Procedure ( Integer) with Width Ada. Integer_Text. _IO. Put ( Item => variable

Put Procedure ( Integer) with Width Ada. Integer_Text. _IO. Put ( Item => variable , Width => field width); Example Ada. Integer_Text. _IO. Put ( Item =>How_Long , Width => 5); See page 93 for more examples

Put Procedure ( Float) with Fore, Aft, Exp Ada. Float_Text. _IO. Put ( Item

Put Procedure ( Float) with Fore, Aft, Exp Ada. Float_Text. _IO. Put ( Item => variable , Fore => n, Aft => n, Exp => n ); Example Ada. Float_Text. _IO. Put ( Item =>Centimeter , Fore => 5, Aft, => 2, Exp => 0); 77. 47 rather than 7. 74700 E+01 See Page 94 for more examples

Data Types and Expressions b Integer • Natural and Positive are subtypes of Integer.

Data Types and Expressions b Integer • Natural and Positive are subtypes of Integer. REM operation • 3 REM 2 is 1 • 17 REM 3 is 2 • 2 REM 3 is 2

Exponentiation • X ** 2 is 9 ( if X is 3 ) •

Exponentiation • X ** 2 is 9 ( if X is 3 ) • X ** 3 is 27 ( if X is 3 ) • Y ** 2 is 1. 44 ( if Y is 1. 2 ) • Y ** 3 is 1. 728 ( if Y is 1. 2) • Y ** 1. 5 is not allowed. The power must be an integer.

Float Literals b A Float literal is a number that begins with a digit

Float Literals b A Float literal is a number that begins with a digit contains a decimal point. Example 3. 14159 0. 0005 1234. 0 15. 0 E-05 -1. 2 E+6 1. 15 E-3 2. 3 E 2 But not 150. 12345. 15 E-03 12. 5 E. 3 -. 123 E 3

Expression Precedence Rules ( more in chap 8) b Parentheses, multiplication, division, addition and

Expression Precedence Rules ( more in chap 8) b Parentheses, multiplication, division, addition and subtraction A. W : = Z * X + Y; B. W : = Z * ( X + Y) ; if X=1, Y=2 and Z= 3, then A will get the result “ 5” and B will be “ 9”.

Example -4 Coin Collection Program WITH Ada. Text_IO; WITH Ada. Integer_Text_IO; PROCEDURE Coin_Collection IS

Example -4 Coin Collection Program WITH Ada. Text_IO; WITH Ada. Integer_Text_IO; PROCEDURE Coin_Collection IS -------------------------------------| Finds the value of a coin collection, --| given pennies and nickels ------------------------------------Pennies : Natural; -- input - number of pennies Nickels : Natural; -- input - number of nickels Dollars : Natural; -- output - value in dollars Cents : Natural; -- output - value in cents Total. Cents : Natural;

Cont - 1 for Example 4 BEGIN -- Coin_Collection -- prompt user for number

Cont - 1 for Example 4 BEGIN -- Coin_Collection -- prompt user for number of nickels and pennies Ada. Text_IO. Put (Item => "How many nickels do you have? "); Ada. Integer_Text_IO. Get (Item => Nickels); Ada. Text_IO. Put (Item => "How many pennies do you have? "); Ada. Integer_Text_IO. Get (Item => Pennies); Ada. Text_IO. New_Line; -- compute total value in cents Total. Cents : = 5 * Nickels + Pennies; -- find the value in dollars and change Dollars : = Total. Cents / 100; Cents : = Total. Cents REM 100;

Cont - 2 for Example 4 -- display the value in dollars and change

Cont - 2 for Example 4 -- display the value in dollars and change Ada. Text_IO. Put (Item => "Your collection is worth "); Ada. Integer_Text_IO. Put (Item => Dollars, Width => 1); Ada. Text_IO. Put (Item => " dollars and "); Ada. Integer_Text_IO. Put (Item => Cents, Width => 1); Ada. Text_IO. Put (" cents. "); Ada. Text_IO. New_Line; END Coin_Collection;