Download presentation
Presentation is loading. Please wait.
Published byPhillip Bridges Modified over 9 years ago
1
Chapter 3 The General Structure of Ada Programs
2
General Form of an Ada Program With package1; With package2;... With packagen; Procedure pname IS - - comments declaration of variables, constants, etc... Begin Program statement; Program statement;... Program statement; End;
3
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;
4
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
5
Type of data b Character b Integer b Float
6
Example-1 Displaying Initials Program WITH Ada.Text_IO; PROCEDURE Hello_Initials IS ------------------------------------------------------------ --| Requests, then displays, user's first and last initials. --| ------------------------------------------------------------ Initial1 : Character; -- objects that hold initials Initial2 : Character;
7
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 => Initial1); Ada.Text_IO.Get(Item => Initial2); -- Display user's initials, with a greeting Ada.Text_IO.Put(Item => "Hello "); Ada.Text_IO.Put(Item => Initial1); Ada.Text_IO.Put(Item => Initial2); Ada.Text_IO.Put(Item => ". Enjoy studying Ada!"); Ada.Text_IO.New_Line; END Hello_Initials;
8
Reserve Words and Identifiers b Reserve Words WithProcedureISBeginEND
9
b Predefined Identifiers Ada.Text_IOPutNew_LineCharacterGet
10
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;
11
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);
12
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);
13
Read Character b To read a character into identifier “Initial1” WITH Ada.Text_IO; Initial1 : Character; Ada.Text._IO.Get ( Item => Initial1);
14
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);
15
Integer and Character output WITH Ada.Integer_Text_IO; Age : Integer; Ada.Integer_Text._IO.Put ( Item => Age); WITH Ada.Text_IO; Initial1 : Character; Ada.Text._IO.Put ( Item => Initial1);
16
Example-2 Displaying the User’s Name WITH Ada.Text_IO; PROCEDURE Hello_Name IS ------------------------------------------------------------------------ --| Requests, then displays, user's name --| ------------------------------------------------------------------------ FirstName: String(1..10); -- object to hold user's name
17
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 => FirstName); -- Display the entered name, with a greeting Ada.Text_IO.Put(Item => "Hello "); Ada.Text_IO.Put(Item => FirstName); Ada.Text_IO.Put(Item => ". Enjoy studying Ada!"); Ada.Text_IO.New_Line; END Hello_Name;
18
Constant Object Declaration Form some_constant : CONSTANT type := value; Example Pi : CONSTANT Float := 3.14159;
19
Assignment Statement Result_Variable := expression; Example X := Y + Z + 2.0; X := X +1;
20
Example-3 Converting Inches to Centimeters WITH Ada.Text_IO; WITH Ada.Float_Text_IO; PROCEDURE Inch_to_CM IS ------------------------------------------------------------------------ --| Converts inches to centimeters ------------------------------------------------------------------------ CMPerInch : CONSTANT Float := 2.54; Inches : Float; Centimeters : Float;
21
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 := CMPerInch * 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;
22
New Line Procedure Form Ada.Text_IO.New_Line ( Spacing => positive number); Example Ada.Text_IO.New_Line ( Spacing =>3);
23
Put Procedure ( Integer) with Width Ada.Integer_Text._IO.Put ( Item => variable, Width => field width); ( Item => variable, Width => field width);ExampleAda.Integer_Text._IO.Put ( Item =>How_Long, Width => 5); ( Item =>How_Long, Width => 5); See page 93 for more examples
24
Put Procedure ( Float) with Fore, Aft, Exp Ada.Float_Text._IO.Put ( Item => variable, Fore => n, Aft => n, Exp => n ); ( Item => variable, Fore => n, Aft => n, Exp => n );ExampleAda.Float_Text._IO.Put ( Item =>Centimeter, Fore => 5, Aft,=> 2, Exp => 0); ( Item =>Centimeter, Fore => 5, Aft,=> 2, Exp => 0); 77.47 rather than 7.74700E+01 See Page 94 for more examples
25
Data Types and Expressions b Integer Natural and Positive are subtypes of Integer.Natural and Positive are subtypes of Integer. REM operation 3 REM 2 is 13 REM 2 is 1 17 REM 3 is 217 REM 3 is 2 2 REM 3 is 22 REM 3 is 2
26
Exponentiation X ** 2 is 9 ( if X is 3 )X ** 2 is 9 ( if X is 3 ) X ** 3 is 27 ( if X is 3 )X ** 3 is 27 ( if X is 3 ) Y ** 2 is 1.44 ( if Y is 1.2 )Y ** 2 is 1.44 ( if Y is 1.2 ) Y ** 3 is 1.728 ( 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.Y ** 1.5 is not allowed. The power must be an integer.
27
Float Literals b A Float literal is a number that begins with a digit contains a decimal point. Example 3.14159 0.00051234.0 15.0E-05 -1.2E+61.15E-32.3E2 But not 150.123412345.15E-03 12.5E.3-.123E3
28
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”.
29
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 TotalCents : Natural;
30
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 TotalCents := 5 * Nickels + Pennies; -- find the value in dollars and change Dollars := TotalCents / 100; Cents := TotalCents REM 100;
31
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;
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.