31
Chapter 3 Chapter 3 The General Structure of The General Structure of Ada Programs Ada Programs

Chapter 3

  • Upload
    helena

  • View
    23

  • Download
    0

Embed Size (px)

DESCRIPTION

Chapter 3. The General Structure of Ada Programs. 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; End;. - PowerPoint PPT Presentation

Citation preview

Page 1: Chapter 3

Chapter 3Chapter 3

The General Structure of Ada The General Structure of Ada ProgramsPrograms

Page 2: Chapter 3

General Form of an Ada General Form of an Ada ProgramProgram

With package1;With package1;

With package2;With package2;

. . . . . .

With packagen;With packagen;

Procedure Procedure pname pname ISIS

- - comments- - comments

declaration of variables, constants, etc. . . declaration of variables, constants, etc. . .

BeginBegin

Program statement;Program statement;

. . .. . .

Program statement;Program statement;

End;End;

Page 3: Chapter 3

With clauseWith clause

To inform the compiler for a package used To inform the compiler for a package used in the program in the program

at least one “with” clause in Ada programat least one “with” clause in Ada program package for Text Input and Outputpackage for Text Input and Output

• WITH Ada.Text_IO;

package for Integer Text Input and Outputpackage for Integer Text Input and Output• WITH Ada.Integer_Text_IO;

package for Floating Text Input and Outputpackage for Floating Text Input and Output• WITH Ada.Float_Text_IO;

Page 4: Chapter 3

Identifiers or namesIdentifiers or names

Begin with a letterBegin with a letter Consist of only letters, digits, and underscoresConsist of only letters, digits, and underscores Cannot use two or more underscore Cannot use two or more underscore

characters in successioncharacters in succession Cannot use an Ada reserved word or pre-Cannot use an Ada reserved word or pre-

defined identifier as an identifier defined identifier as an identifier

Pick meaningful names for identifiersPick meaningful names for identifiers no restriction on the length of an identifierno restriction on the length of an identifier

Page 5: Chapter 3

Type of dataType of data

CharacterCharacter IntegerInteger FloatFloat

Page 6: Chapter 3

Example-1Example-1Displaying Initials ProgramDisplaying 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;

Page 7: Chapter 3

Cont. of example-1Cont. 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;

Page 8: Chapter 3

Reserve Words and Reserve Words and IdentifiersIdentifiers

Reserve WordsReserve Words

With With

ProcedureProcedure

ISIS

BeginBegin

END END

Page 9: Chapter 3

Predefined IdentifiersPredefined Identifiers

Ada.Text_IOAda.Text_IO

PutPut

New_LineNew_Line

CharacterCharacter

GetGet

Page 10: Chapter 3

Input and Output Input and Output

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

package for Integer Text Input and package for Integer Text Input and OutputOutput• WITH Ada.Integer_Text_IO;

package for Floating Text Input package for Floating Text Input and Outputand Output• WITH Ada.Float_Text_IO;

Page 11: Chapter 3

Read Float NumberRead Float Number

To read a float number into identifier To read a float number into identifier “Inches”“Inches”

WITH Ada.Float_Text_IO;

Inches : Float;Inches : Float;

Ada.Ada.FloatFloat_Text._IO._Text._IO.GetGet ( Item => Inches); ( Item => Inches);

Page 12: Chapter 3

Read Integer NumberRead Integer Number

To read an integer number into identifier To read an integer number into identifier “Age”“Age”

WITH Ada.Integer_Text_IO;

Age : Integer;Age : Integer;

Ada.Ada.IntegerInteger_Text._IO._Text._IO.GetGet ( Item => Age); ( Item => Age);

Page 13: Chapter 3

Read CharacterRead Character

To read a character into identifier To read a character into identifier “Initial1”“Initial1”

WITH Ada.Text_IO;

Initial1 : Character;Initial1 : Character;

Ada.Text._IO.Ada.Text._IO.GetGet ( Item => Initial1); ( Item => Initial1);

Page 14: Chapter 3

Write Float NumberWrite Float Number

To write a float number into identifier To write a float number into identifier “Inches”“Inches”

WITH Ada.Float_Text_IO;

Inches : Float;Inches : Float;

Ada.Ada.FloatFloat_Text._IO._Text._IO.PutPut ( Item => Inches); ( Item => Inches);

Page 15: Chapter 3

Integer and Character Integer and Character outputoutput

WITH Ada.Integer_Text_IO;

Age : Integer;Age : Integer;

Ada.Ada.IntegerInteger_Text._IO._Text._IO.PutPut ( Item => Age); ( Item => Age);

WITH Ada.Text_IO;

Initial1 : Character;Initial1 : Character;

Ada.Text._IO.Ada.Text._IO.PutPut ( Item => Initial1); ( Item => Initial1);

Page 16: Chapter 3

Example-2Example-2Displaying the User’s NameDisplaying 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

Page 17: Chapter 3

Cont. of example 2Cont. 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;

Page 18: Chapter 3

Constant Object DeclarationConstant Object Declaration

FormForm

some_constantsome_constant : CONSTANT : CONSTANT type type :=:= valuevalue;;

ExampleExample

Pi : CONSTANT Float := 3.14159;Pi : CONSTANT Float := 3.14159;

Page 19: Chapter 3

Assignment StatementAssignment Statement

Result_Variable := expression;Result_Variable := expression;

ExampleExample

X := Y + Z + 2.0;X := Y + Z + 2.0;

X := X +1;X := X +1;

Page 20: Chapter 3

Example-3Example-3Converting Inches to Converting Inches to CentimetersCentimeters

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;

Page 21: Chapter 3

Cont. of example-3Cont. 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;

Page 22: Chapter 3

New Line ProcedureNew Line Procedure

FormForm

Ada.Text_IO.New_Line ( Spacing => positive Ada.Text_IO.New_Line ( Spacing => positive number);number);

ExampleExample

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

Page 23: Chapter 3

Put Procedure ( Integer) with Put Procedure ( Integer) with WidthWidth

Ada.Integer_Text._IO.Put Ada.Integer_Text._IO.Put

( Item => variable , ( Item => variable , Width => field widthWidth => field width););

ExampleExample

Ada.Integer_Text._IO.Put Ada.Integer_Text._IO.Put

( Item =>How_Long , ( Item =>How_Long , Width => 5Width => 5););

See page 93 for more examples See page 93 for more examples

Page 24: Chapter 3

Put Procedure ( Float) with Fore, Put Procedure ( Float) with Fore, Aft, ExpAft, Exp

Ada.Float_Text._IO.Put Ada.Float_Text._IO.Put

( Item => variable ,( Item => variable , Fore Fore =>=> n n,, Aft Aft =>=> n n, Exp, Exp =>=> n n ););

ExampleExample

Ada.Float_Text._IO.Put Ada.Float_Text._IO.Put

( Item =>Centimeter , Fore => 5, Aft,=> 2, Exp => ( Item =>Centimeter , Fore => 5, Aft,=> 2, Exp => 0);0);

77.47 rather than 7.74700E+0177.47 rather than 7.74700E+01

See Page 94 for more examplesSee Page 94 for more examples

Page 25: Chapter 3

Data Types and Data Types and ExpressionsExpressions

IntegerInteger• NaturalNatural and and PositivePositive are subtypes of are subtypes of

Integer.Integer.

REM operationREM 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

Page 26: Chapter 3

ExponentiationExponentiation• 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 Y ** 1.5 is not allowed . The power

must be an integer.must be an integer.

Page 27: Chapter 3

Float LiteralsFloat Literals

A Float literal is a number that begins A Float literal is a number that begins with a digit contains a decimal point.with a digit contains a decimal point.

ExampleExample

3.141593.14159 0.0005 0.0005 1234.01234.0 15.0E-05 15.0E-05

-1.2E+6-1.2E+6 1.15E-31.15E-3 2.3E22.3E2

But notBut not

150150 .1234.1234 12345.12345. 15E-0315E-03

12.5E.312.5E.3 -.123E3-.123E3

Page 28: Chapter 3

ExpressionExpression

Precedence Rules ( more in chap 8)Precedence Rules ( more in chap 8) Parentheses, multiplication, division, Parentheses, multiplication, division,

addition and subtractionaddition and subtraction

A.A. W := Z * X + Y;W := Z * X + Y;

B.B. W := Z * ( X + Y) ;W := Z * ( X + Y) ;

if X=1, Y=2 and Z= 3, then if X=1, Y=2 and Z= 3, then

A will get the result “5” and B will be “9”.A will get the result “5” and B will be “9”.

Page 29: Chapter 3

Example -4 Coin Collection Example -4 Coin Collection ProgramProgram

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;

Page 30: Chapter 3

Cont - 1 for Example 4Cont - 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;

Page 31: Chapter 3

Cont - 2 for Example 4Cont - 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;