chapter 6 counting loops; subtypes in preceding chapters, 1.sequence and 2.conditional structures in...

66
Chapter 6 Chapter 6 Counting Loops; Counting Loops; Subtypes Subtypes In preceding chapters, In preceding chapters, 1. 1. Sequence and Sequence and 2. 2. Conditional Conditional structures structures In this chapter, In this chapter, 3. 3. Repetition, or Repetition, or

Upload: conrad-holt

Post on 17-Jan-2016

249 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Chapter 6 Counting Loops; Subtypes In preceding chapters, 1.Sequence and 2.Conditional structures In this chapter, 3.Repetition, or iteration

Chapter 6Chapter 6

Counting Loops; SubtypesCounting Loops; Subtypes

In preceding chapters, In preceding chapters,

1.1. Sequence andSequence and

2.2. Conditional structuresConditional structures

In this chapter,In this chapter,

3.3. Repetition, or iteration Repetition, or iteration

Page 2: Chapter 6 Counting Loops; Subtypes In preceding chapters, 1.Sequence and 2.Conditional structures In this chapter, 3.Repetition, or iteration

LOOPLOOP

- Repetition of steps in a program- Repetition of steps in a program

- Loop body contains the steps to be - Loop body contains the steps to be repeatedrepeated

Page 3: Chapter 6 Counting Loops; Subtypes In preceding chapters, 1.Sequence and 2.Conditional structures In this chapter, 3.Repetition, or iteration

Example 1Example 1

Ada.Text_IO.Put ( Item => “ Hello there. “ );Ada.Text_IO.Put ( Item => “ Hello there. “ );

Ada.Text_IO.Put ( Item => “ Hello there. “ );Ada.Text_IO.Put ( Item => “ Hello there. “ );

Ada.Text_IO.Put ( Item => “ Hello there. “ );Ada.Text_IO.Put ( Item => “ Hello there. “ );

Ada.Text_IO.Put ( Item => “ Hello there. “ );Ada.Text_IO.Put ( Item => “ Hello there. “ );

Ada.Text_IO.Put ( Item => “ Hello there. “ );Ada.Text_IO.Put ( Item => “ Hello there. “ );

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

FOR Count IN 1..5 LOOPFOR Count IN 1..5 LOOP

Ada.Text_IO.Put ( Item => “ Hello there. “ );Ada.Text_IO.Put ( Item => “ Hello there. “ );

END LOOP;END LOOP;

Page 4: Chapter 6 Counting Loops; Subtypes In preceding chapters, 1.Sequence and 2.Conditional structures In this chapter, 3.Repetition, or iteration

Example 2Example 2

How could we write to print numbers as follow:How could we write to print numbers as follow:

11

22

33

..

..

100100

Page 5: Chapter 6 Counting Loops; Subtypes In preceding chapters, 1.Sequence and 2.Conditional structures In this chapter, 3.Repetition, or iteration

Here is what we learned Here is what we learned so farso far

I:= 1;I:= 1;

Ada.Integer_Text_IO ( Item => I );Ada.Integer_Text_IO ( Item => I );

Ada.Text_IO.New_Line;Ada.Text_IO.New_Line;

I := I +1;I := I +1;

Ada.Integer_Text_IO ( Item => I );Ada.Integer_Text_IO ( Item => I );

Ada.Text_IO.New_Line;Ada.Text_IO.New_Line;

I := I +1;I := I +1;

Ada.Integer_Text_IO ( Item => I );Ada.Integer_Text_IO ( Item => I );

Ada.Text_IO.New_Line;Ada.Text_IO.New_Line;

..

..

Page 6: Chapter 6 Counting Loops; Subtypes In preceding chapters, 1.Sequence and 2.Conditional structures In this chapter, 3.Repetition, or iteration

Using FOR statementUsing FOR statement

FOR I In 1. . 100 LOOPFOR I In 1. . 100 LOOP

Ada.Integer_Text_IO ( Item => Ada.Integer_Text_IO ( Item => I );I );

Ada.Text_IO.New_Line; Ada.Text_IO.New_Line;

END LOOP;END LOOP;

Page 7: Chapter 6 Counting Loops; Subtypes In preceding chapters, 1.Sequence and 2.Conditional structures In this chapter, 3.Repetition, or iteration

FOR statementFOR statement

FOR FOR countercounter IN IN lowboundlowbound . . . . HighboundHighbound LOOP LOOP

statement sequencestatement sequence

END LOOPEND LOOP

FOR FOR countercounter IN REVERSE IN REVERSE lowboundlowbound . . . . HighboundHighbound LOOP LOOP

statement sequencestatement sequence

END LOOPEND LOOP

Page 8: Chapter 6 Counting Loops; Subtypes In preceding chapters, 1.Sequence and 2.Conditional structures In this chapter, 3.Repetition, or iteration

To list Number, Square and CubeTo list Number, Square and CubeStarting from 1Starting from 1

WITH Ada.Text_IO;WITH Ada.Integer_Text_IO;Procedure Square_Cube Is----------------------------- to display square and cube of a number from 1 to n-- Input max (say here 10 )-- Output number square cube-- As follows:-- Number Square Cube-- 1 1 1-- 2 4 8-- 3 9 27-- . . .-- . . .

-- 10 100 1000 ---------------------------

Page 9: Chapter 6 Counting Loops; Subtypes In preceding chapters, 1.Sequence and 2.Conditional structures In this chapter, 3.Repetition, or iteration

Cont. 1 of square_cube Cont. 1 of square_cube

max, Square, Cube : natural;Begin -- Ada.Text_IO.Put ( Item => " Give maximum value to find square

and cube "); Ada.Text_IO.New_Line; Ada.Integer_Text_IO.Get ( Item => max );

Ada.Text_IO.Put ( Item => " Num Square Cube "); Ada.Text_IO.New_Line; Ada.Text_IO.Put ( Item => " --- ------ ------ "); Ada.Text_IO.New_Line;

Page 10: Chapter 6 Counting Loops; Subtypes In preceding chapters, 1.Sequence and 2.Conditional structures In this chapter, 3.Repetition, or iteration

Cont. 2 of square_cubeCont. 2 of square_cube

FOR Num IN 1..max LOOP Square := Num * Num; Cube := Num * Num * Num; Ada.Integer_Text_IO.Put ( Item => Num,

width=>10); Ada.Integer_Text_IO.Put ( Item => Square,

width=>10); Ada.Integer_Text_IO.Put ( Item => Cube,

width=>10); Ada.Text_IO.New_Line; END LOOP;

END Square_Cube;

Page 11: Chapter 6 Counting Loops; Subtypes In preceding chapters, 1.Sequence and 2.Conditional structures In this chapter, 3.Repetition, or iteration

Find the sum of all integers from Find the sum of all integers from 1 to N1 to N

WITH Ada.Text_IO;WITH Ada.Integer_Text_IO;Procedure Sum_Integer Is----------------------------- to display sum of all integers from 1 to N-- Input N -- Output the sum of 1 to N

---------------------------

N : Positive; ---- inputSum : Natural; ---- output

Page 12: Chapter 6 Counting Loops; Subtypes In preceding chapters, 1.Sequence and 2.Conditional structures In this chapter, 3.Repetition, or iteration

Cont.Cont. 1 of finding sum1 of finding sumBegin -- Ada.Text_IO.Put ( Item => " Enter the last integer in the sum: ”

"); Ada.Integer_Text_IO.Get ( Item => N ); ----- to find the sum from 1 to N Sum := 0; ------ initialize sum to 0 FOR I IN 1..N LOOP Sum := Sum + I; ----- add next integer to sum END LOOP;Ada.Text_IO.Put ( Item => " The sum of the integers from 1 to ");Ada.Integer_Text_IO.Put ( Item => N, Width =>1);Ada.Text_IO.Put ( Item => " is ");Ada.Integer_Text_IO.Put ( Item => Sum, Width =>1)Ada.Text_IO.New_Line;END Sum_Integers ;

Page 13: Chapter 6 Counting Loops; Subtypes In preceding chapters, 1.Sequence and 2.Conditional structures In this chapter, 3.Repetition, or iteration

Nested LoopNested Loop

Nested Loop consists of Nested Loop consists of

an outer loop an outer loop

with one or more inner with one or more inner loops.loops.

Each time the outer loop is repeated,Each time the outer loop is repeated,

the inner loops are reentered. the inner loops are reentered.

Their loop control parameters Their loop control parameters

are reevaluated andare reevaluated and

required iterations are performed.required iterations are performed.

Page 14: Chapter 6 Counting Loops; Subtypes In preceding chapters, 1.Sequence and 2.Conditional structures In this chapter, 3.Repetition, or iteration

Example-1Example-1

For I in 1..5 LoopFor I in 1..5 Loop

For J in 1..3 LoopFor J in 1..3 Loop

Ada.Integer_Text_IO.Put( Item=>I, width=>1);Ada.Integer_Text_IO.Put( Item=>I, width=>1);

Ada.Integer_Text_IO.Put( Item=>J, width=>1);Ada.Integer_Text_IO.Put( Item=>J, width=>1);

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

End Loop; - - - - inner loop finishedEnd Loop; - - - - inner loop finished

Ada.Text_IO.New_Line;Ada.Text_IO.New_Line;

End Loop; - - - - outer loop finishedEnd Loop; - - - - outer loop finished

What will be displayed?What will be displayed?

Page 15: Chapter 6 Counting Loops; Subtypes In preceding chapters, 1.Sequence and 2.Conditional structures In this chapter, 3.Repetition, or iteration

Output for Example - 1Output for Example - 1

11 12 1311 12 13

21 22 2321 22 23

31 32 3331 32 33

41 42 4341 42 43

51 52 5351 52 53

Page 16: Chapter 6 Counting Loops; Subtypes In preceding chapters, 1.Sequence and 2.Conditional structures In this chapter, 3.Repetition, or iteration

Example -2Example -2

Sum := 0;Sum := 0;

For I in 1..5 LoopFor I in 1..5 Loop

For J in 1..3 LoopFor J in 1..3 Loop

Sum := I + J;Sum := I + J;

Ada. Text_IO.Put( Item=> “I + J = “);Ada. Text_IO.Put( Item=> “I + J = “);

Ada.Integer_Text_IO.Put( Item=>Sum, width=>1);Ada.Integer_Text_IO.Put( Item=>Sum, width=>1);

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

End Loop; - - - - inner loop finishedEnd Loop; - - - - inner loop finished

Ada.Text_IO.New_Line;Ada.Text_IO.New_Line;

End Loop; - - - - outer loop finishedEnd Loop; - - - - outer loop finished

What will be displayed?What will be displayed?

Page 17: Chapter 6 Counting Loops; Subtypes In preceding chapters, 1.Sequence and 2.Conditional structures In this chapter, 3.Repetition, or iteration

Output for example-2Output for example-2

I + J = 2, I + J = 3, I + J = 4,I + J = 2, I + J = 3, I + J = 4,

I + J = 3, I + J = 4, I + J = 5,I + J = 3, I + J = 4, I + J = 5,

I + J = 4, I + J = 5, I + J = 6,I + J = 4, I + J = 5, I + J = 6,

I + J = 5, I + J = 6, I + J = 7,I + J = 5, I + J = 6, I + J = 7,

I + J = 6, I + J = 7, I + J = 8,I + J = 6, I + J = 7, I + J = 8,

Page 18: Chapter 6 Counting Loops; Subtypes In preceding chapters, 1.Sequence and 2.Conditional structures In this chapter, 3.Repetition, or iteration

Example - 3Example - 3

For I in 1..5 LoopFor I in 1..5 Loop

Sum := 0;Sum := 0;

For J in 1..3 LoopFor J in 1..3 Loop

Sum := I + J;Sum := I + J;

Ada. Text_IO.Put( Item=> “I + J = “);Ada. Text_IO.Put( Item=> “I + J = “);

Ada.Integer_Text_IO.Put( Item=>Sum, width=>1);Ada.Integer_Text_IO.Put( Item=>Sum, width=>1);

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

End Loop; - - - - inner loop finishedEnd Loop; - - - - inner loop finished

Ada.Text_IO.New_Line;Ada.Text_IO.New_Line;

End Loop; - - - - outer loop finishedEnd Loop; - - - - outer loop finished

What will be displayed?What will be displayed?

Page 19: Chapter 6 Counting Loops; Subtypes In preceding chapters, 1.Sequence and 2.Conditional structures In this chapter, 3.Repetition, or iteration

Output for Exmple -3Output for Exmple -3

I + J = 2, I + J = 3, I + J = 4,I + J = 2, I + J = 3, I + J = 4,

I + J = 3, I + J = 4, I + J = 5,I + J = 3, I + J = 4, I + J = 5,

I + J = 4, I + J = 5, I + J = 6,I + J = 4, I + J = 5, I + J = 6,

I + J = 5, I + J = 6, I + J = 7,I + J = 5, I + J = 6, I + J = 7,

I + J = 6, I + J = 7, I + J = 8,I + J = 6, I + J = 7, I + J = 8,

Page 20: Chapter 6 Counting Loops; Subtypes In preceding chapters, 1.Sequence and 2.Conditional structures In this chapter, 3.Repetition, or iteration

Example - 4Example - 4

Sum := 0;Sum := 0;

For I in 1..5 LoopFor I in 1..5 Loop

For J in 1..3 LoopFor J in 1..3 Loop

Sum :=Sum + I + J;Sum :=Sum + I + J;

Ada. Text_IO.Put( Item=> “I + J = “);Ada. Text_IO.Put( Item=> “I + J = “);

Ada.Integer_Text_IO.Put( Item=>Sum, width=>1);Ada.Integer_Text_IO.Put( Item=>Sum, width=>1);

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

End Loop; - - - - inner loop finishedEnd Loop; - - - - inner loop finished

Ada.Text_IO.New_Line;Ada.Text_IO.New_Line;

End Loop; - - - - outer loop finishedEnd Loop; - - - - outer loop finished

What will be displayed?What will be displayed?

Page 21: Chapter 6 Counting Loops; Subtypes In preceding chapters, 1.Sequence and 2.Conditional structures In this chapter, 3.Repetition, or iteration

Output for Example-4Output for Example-4

I + J = 2, I + J = 3, I + J = 4,I + J = 2, I + J = 3, I + J = 4,

I + J = 7, I + J = 8, I + J = 9,I + J = 7, I + J = 8, I + J = 9,

I + J = 13, I + J = 14, I + J = 15,I + J = 13, I + J = 14, I + J = 15,

I + J = 20, I + J = 21, I + J = 22,I + J = 20, I + J = 21, I + J = 22,

I + J = 28, I + J = 29, I + J = 30,I + J = 28, I + J = 29, I + J = 30,

Page 22: Chapter 6 Counting Loops; Subtypes In preceding chapters, 1.Sequence and 2.Conditional structures In this chapter, 3.Repetition, or iteration

Example-5Example-5

For I in 1..5 LoopFor I in 1..5 Loop

Sum := 0;Sum := 0;

For J in 1..3 LoopFor J in 1..3 Loop

Sum :=Sum + I + J;Sum :=Sum + I + J;

Ada. Text_IO.Put( Item=> “I + J = “);Ada. Text_IO.Put( Item=> “I + J = “);

Ada.Integer_Text_IO.Put( Item=>Sum, width=>1);Ada.Integer_Text_IO.Put( Item=>Sum, width=>1);

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

End Loop; - - - - inner loop finishedEnd Loop; - - - - inner loop finished

Ada.Text_IO.New_Line;Ada.Text_IO.New_Line;

End Loop; - - - - outer loop finishedEnd Loop; - - - - outer loop finished

What will be displayed?What will be displayed?

Page 23: Chapter 6 Counting Loops; Subtypes In preceding chapters, 1.Sequence and 2.Conditional structures In this chapter, 3.Repetition, or iteration

Output for Example - 5Output for Example - 5

I + J = 2, I + J = 5, I + J = 9,I + J = 2, I + J = 5, I + J = 9,

I + J = 3, I + J = 7, I + J = 12,I + J = 3, I + J = 7, I + J = 12,

I + J = 4, I + J = 9, I + J = 15,I + J = 4, I + J = 9, I + J = 15,

I + J = 5, I + J = 11, I + J = 17,I + J = 5, I + J = 11, I + J = 17,

I + J = 6, I + J = 13, I + J = 19,I + J = 6, I + J = 13, I + J = 19,

Page 24: Chapter 6 Counting Loops; Subtypes In preceding chapters, 1.Sequence and 2.Conditional structures In this chapter, 3.Repetition, or iteration

Example - 6Example - 6

For I in 1..5 LoopFor I in 1..5 Loop

For J in 1..3 LoopFor J in 1..3 Loop

Sum := 0;Sum := 0;

Sum :=Sum + I + J;Sum :=Sum + I + J;

Ada. Text_IO.Put( Item=> “I + J = “);Ada. Text_IO.Put( Item=> “I + J = “);

Ada.Integer_Text_IO.Put( Item=>Sum, width=>1);Ada.Integer_Text_IO.Put( Item=>Sum, width=>1);

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

End Loop; - - - - inner loop finishedEnd Loop; - - - - inner loop finished

Ada.Text_IO.New_Line;Ada.Text_IO.New_Line;

End Loop; - - - - outer loop finishedEnd Loop; - - - - outer loop finished

What will be displayed?What will be displayed?

Page 25: Chapter 6 Counting Loops; Subtypes In preceding chapters, 1.Sequence and 2.Conditional structures In this chapter, 3.Repetition, or iteration

Output for Example - 6Output for Example - 6

I + J = 2, I + J = 3, I + J = 4,I + J = 2, I + J = 3, I + J = 4,

I + J = 3, I + J = 4, I + J = 5,I + J = 3, I + J = 4, I + J = 5,

I + J = 4, I + J = 5, I + J = 6,I + J = 4, I + J = 5, I + J = 6,

I + J = 5, I + J = 6, I + J = 7,I + J = 5, I + J = 6, I + J = 7,

I + J = 6, I + J = 7, I + J = 8,I + J = 6, I + J = 7, I + J = 8,

Page 26: Chapter 6 Counting Loops; Subtypes In preceding chapters, 1.Sequence and 2.Conditional structures In this chapter, 3.Repetition, or iteration

Example - - - Outer- Inner Example - - - Outer- Inner LoopLoop

BEGIN -- Nested_Loops

Ada.Text_IO.Put (Item => " OuterCounter InnerCounter");

Ada.Text_IO.New_Line;

Page 27: Chapter 6 Counting Loops; Subtypes In preceding chapters, 1.Sequence and 2.Conditional structures In this chapter, 3.Repetition, or iteration

Cont. Outer - Inner LoopCont. Outer - Inner Loop

FOR OuterCounter IN 1 .. 3 LOOP

Ada.Text_IO.Put(Item => "OUTER"); Ada.Integer_Text_IO.Put (Item => OuterCounter, Width =>

10); Ada.Text_IO.New_Line;

FOR InnerCounter IN 1 .. OuterCounter LOOP Ada.Text_IO.Put(Item => " INNER"); Ada.Integer_Text_IO.Put (Item => InnerCounter, Width =>

22); Ada.Text_IO.New_Line; END LOOP; END LOOP;END Nested_Loops;

Page 28: Chapter 6 Counting Loops; Subtypes In preceding chapters, 1.Sequence and 2.Conditional structures In this chapter, 3.Repetition, or iteration

Example - - - TriangleExample - - - Triangle

NumLines: CONSTANT Integer := 5; Blank : CONSTANT Character := ' '; Star : CONSTANT Character := '*';

Page 29: Chapter 6 Counting Loops; Subtypes In preceding chapters, 1.Sequence and 2.Conditional structures In this chapter, 3.Repetition, or iteration

Cont. .. TriangleCont. .. TriangleBEGIN -- Triangle

FOR Row IN 1 .. NumLines LOOP -- draw each row

FOR LeadBlanks IN REVERSE 1 .. NumLines - Row LOOP Ada.Text_IO.Put(Item => Blank); -- leading blanks END LOOP;

FOR CountStars IN 1 .. (2*Row) - 1 LOOP Ada.Text_IO.Put(Item => Star); -- display asterisks END LOOP; Ada.Text_IO.New_Line; -- terminate row

END LOOP;END Triangle;

Page 30: Chapter 6 Counting Loops; Subtypes In preceding chapters, 1.Sequence and 2.Conditional structures In this chapter, 3.Repetition, or iteration

Subtype of Enumeration Subtype of Enumeration TypesTypes

Subtype of programmer’s defined Subtype of programmer’s defined typestypes

can be defined just as easily as can be defined just as easily as

subtypes of predefined types.subtypes of predefined types.

Page 31: Chapter 6 Counting Loops; Subtypes In preceding chapters, 1.Sequence and 2.Conditional structures In this chapter, 3.Repetition, or iteration

ExampleExample

Type Months IS ( Jan, Feb, Mar, Apr, May, Jun,Type Months IS ( Jan, Feb, Mar, Apr, May, Jun,

Jul, Aug, Sep, Oct, Jul, Aug, Sep, Oct, Nov, Dec);Nov, Dec);

Subtype Spring IS Month Range Mar. . May;Subtype Spring IS Month Range Mar. . May;

Subtype Summer IS Month Range Jun . . Aug;Subtype Summer IS Month Range Jun . . Aug;

Subtype Autumn IS Month Range Sep . . Subtype Autumn IS Month Range Sep . . Nov;Nov;

Page 32: Chapter 6 Counting Loops; Subtypes In preceding chapters, 1.Sequence and 2.Conditional structures In this chapter, 3.Repetition, or iteration

NoteNote

Ada requires that the values of a subtype Ada requires that the values of a subtype be specified in the form of a range and be specified in the form of a range and therefore therefore contiguouscontiguous, ,

that is, adjacent in the base type that is, adjacent in the base type definition.definition.

Can we define as follow ?Can we define as follow ?

Subtype Winter IS Months Range Dec. . Subtype Winter IS Months Range Dec. . Feb; Feb;

Page 33: Chapter 6 Counting Loops; Subtypes In preceding chapters, 1.Sequence and 2.Conditional structures In this chapter, 3.Repetition, or iteration

Example WorkDayExample WorkDay

WITH Ada.Text_IO;PROCEDURE Work_Days IS----------------------------------------------------------------| Demonstrates the use of enumeration subtypes:--| prompts user for a day of the week and determines whether--| the following day is a weekday or weekend day.--------------------------------------------------------------

TYPE Days IS (Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday); SUBTYPE WeekDays IS Days RANGE Monday .. Friday; SUBTYPE WeekEnd IS Days RANGE Saturday .. Sunday; PACKAGE Day_IO IS NEW Ada.Text_IO.Enumeration_IO (Enum => Days);

Page 34: Chapter 6 Counting Loops; Subtypes In preceding chapters, 1.Sequence and 2.Conditional structures In this chapter, 3.Repetition, or iteration

Cont.. ExampleCont.. Example

Today : Days; -- input - day of the week Tomorrow : Days; -- output - next day

BEGIN -- Work_Days -- prompt user to enter a day name Ada.Text_IO.Put (Item => "Enter a day of the week > "); Day_IO.Get (Item => Today); -- find tomorrow IF Today = Days'Last THEN Tomorrow := Days'First; ELSE Tomorrow := Days'Succ(Today); END IF;

Page 35: Chapter 6 Counting Loops; Subtypes In preceding chapters, 1.Sequence and 2.Conditional structures In this chapter, 3.Repetition, or iteration

Cont.. ExampleCont.. Example

Ada.Text_IO.Put (Item => "Tomorrow is "); Day_IO.Put (Item => Tomorrow); Ada.Text_IO.New_Line;-- Is Tomorrow a week day or a weekend day? IF Tomorrow IN Weekdays THEN Ada.Text_IO.Put (Item => "Another day, another dollar..."); Ada.Text_IO.New_Line; ELSE Ada.Text_IO.Put (Item => "We've worked hard, let's play

hard!"); Ada.Text_IO.New_Line; END IF; Ada.Text_IO.Put (Item => "Have a good day tomorrow."); Ada.Text_IO.New_Line;END Work_Days;

Page 36: Chapter 6 Counting Loops; Subtypes In preceding chapters, 1.Sequence and 2.Conditional structures In this chapter, 3.Repetition, or iteration

Addition TableAddition Table

WITH Ada.Text_IO;WITH Ada.Integer_Text_IO;PROCEDURE Addition_Table IS-------------------------------------------------------

---------| Displays an addition table.--|

--------------------------------------------------------------

MaxDigit : CONSTANT Natural := 9; SUBTYPE SmallNatural IS Natural RANGE 0 .. MaxDigit;

Page 37: Chapter 6 Counting Loops; Subtypes In preceding chapters, 1.Sequence and 2.Conditional structures In this chapter, 3.Repetition, or iteration

Cont.. Addition TableCont.. Addition Table

BEGIN -- Addition_Table

-- Display the table heading. Ada.Text_IO.Put(Item => "+"); FOR Right IN SmallNatural LOOP -- Display heading Ada.Integer_Text_IO.Put(Item => Right, Width => 3); END LOOP; Ada.Text_IO.New_Line; -- Terminate

heading

Page 38: Chapter 6 Counting Loops; Subtypes In preceding chapters, 1.Sequence and 2.Conditional structures In this chapter, 3.Repetition, or iteration

Cont.. Addition TableCont.. Addition Table

-- Display the table body. FOR Left IN SmallNatural LOOP

-- Display each row of the table Ada.Integer_Text_IO.Put(Item => Left, Width => 1);

FOR Right IN SmallNatural LOOP Ada.Integer_Text_IO.Put (Item => Left + Right, Width

=> 3); END LOOP; Ada.Text_IO.New_Line; -- Terminate table row END LOOP;END Addition_Table;

Page 39: Chapter 6 Counting Loops; Subtypes In preceding chapters, 1.Sequence and 2.Conditional structures In this chapter, 3.Repetition, or iteration

Format for FOR statementFormat for FOR statement

FOR counter IN type-name LOOPFOR counter IN type-name LOOP

statement sequencestatement sequence

END LOOP;END LOOP;

FOR counter IN REVERSE type-name FOR counter IN REVERSE type-name LOOPLOOP

statement sequencestatement sequence

END LOOP;END LOOP;

Page 40: Chapter 6 Counting Loops; Subtypes In preceding chapters, 1.Sequence and 2.Conditional structures In this chapter, 3.Repetition, or iteration

Limitation of the FOR Limitation of the FOR statementstatement

Counter is always Counter is always

eithereither incremented ( by taking the successor)incremented ( by taking the successor) decremented ( by taking the predecessor)decremented ( by taking the predecessor)

Only to loop through Only to loop through

all the values of a given rangeall the values of a given range

no way to loop alternate counter no way to loop alternate counter

such as by 2such as by 2

Page 41: Chapter 6 Counting Loops; Subtypes In preceding chapters, 1.Sequence and 2.Conditional structures In this chapter, 3.Repetition, or iteration

Using an External File Using an External File for Input Datafor Input Data

Create a data file in disk using an Create a data file in disk using an editor (vi)editor (vi)

Declare the name of the file to be used Declare the name of the file to be used in programin program

Associate the name of the file in the Associate the name of the file in the program with the name of the file in program with the name of the file in disk. disk.

Write Input Operation to read data from Write Input Operation to read data from file file

Page 42: Chapter 6 Counting Loops; Subtypes In preceding chapters, 1.Sequence and 2.Conditional structures In this chapter, 3.Repetition, or iteration

1. 1. Create a data file in disk using an Create a data file in disk using an editor (vi)editor (vi)

ExampleExample

>vi scores.dat>vi scores.dat

55

5757

2222

100100

7676

8282

Page 43: Chapter 6 Counting Loops; Subtypes In preceding chapters, 1.Sequence and 2.Conditional structures In this chapter, 3.Repetition, or iteration

2. 2. Declare the name of the file to be Declare the name of the file to be used in programused in program

DeclarationDeclaration

TestScore : Ada.Text_IO.File_Type;TestScore : Ada.Text_IO.File_Type;

Page 44: Chapter 6 Counting Loops; Subtypes In preceding chapters, 1.Sequence and 2.Conditional structures In this chapter, 3.Repetition, or iteration

3.3. Associate the name of the file in the Associate the name of the file in the program with the name of the file in program with the name of the file in

disk.disk.

Ada.Text_IO.OpenAda.Text_IO.Open

( File => TestScore, Mode => ( File => TestScore, Mode => Ada.Text_IO.In_File,Ada.Text_IO.In_File,

Name => “score.dat” );Name => “score.dat” );

Page 45: Chapter 6 Counting Loops; Subtypes In preceding chapters, 1.Sequence and 2.Conditional structures In this chapter, 3.Repetition, or iteration

4.4. Write Input Operation to read data Write Input Operation to read data from filefrom file

Ada.Integer_Text_IO.Get (File => Ada.Integer_Text_IO.Get (File => TestScore, Item => CurrentValue);TestScore, Item => CurrentValue);

Page 46: Chapter 6 Counting Loops; Subtypes In preceding chapters, 1.Sequence and 2.Conditional structures In this chapter, 3.Repetition, or iteration

WITH Ada.Text_IO;WITH Ada.Integer_Text_IO;WITH Min_Max;PROCEDURE Min_Max_Average_File IS--------------------------------------------------------------------------| Finds and displays the minimum, maximum, and average--| of a list of data items; the data items are read from a file.--| ------------------------------------------------------------------------NumValues: Positive; -- input - the number of items to be averaged CurrentValue: Integer; -- input - the next data item to be added Smallest: Integer; -- output - minimum of the data values Largest: Integer; -- output - maximum of the data values Average: Integer; -- output - average of the data values Sum: Integer; -- program variable - sum being accumulated TestScores: Ada.Text_IO.File_Type; -- program variable - names the input file --

Step 2

Page 47: Chapter 6 Counting Loops; Subtypes In preceding chapters, 1.Sequence and 2.Conditional structures In this chapter, 3.Repetition, or iteration

BEGIN -- Min_Max_Average_File -- Open the file and associate it with the file variable name --

Step 3 Ada.Text_IO.Open (File => TestScores, Mode => Ada.Text_IO.In_File, Name => "scores.dat"); -- Read from the file the number of items to be averaged -- Step 4 Ada.Integer_Text_IO.Get(File => TestScores, Item => NumValues); Ada.Text_IO.Put("The number of scores to be averaged is "); Ada.Integer_Text_IO.Put(Item => NumValues, Width => 1); Ada.Text_IO.New_Line; -- Initialize program variables Smallest := Integer'Last; Largest := Integer'First; Sum := 0;

Page 48: Chapter 6 Counting Loops; Subtypes In preceding chapters, 1.Sequence and 2.Conditional structures In this chapter, 3.Repetition, or iteration

-- Initialize program variables

Smallest := Integer'Last; Largest := Integer'First; Sum := 0;

-- Read each data item, log to the screen, add it to Sum, -- and check if it is a new minimum or maximum FOR Count IN 1 .. NumValues LOOP Ada.Integer_Text_IO.Get(File => TestScores, Item => CurrentValue); Ada.Text_IO.Put("Score number "); Ada.Integer_Text_IO.Put(Item => Count, Width => 1); Ada.Text_IO.Put(" is "); Ada.Integer_Text_IO.Put(Item => CurrentValue, Width => 1); Ada.Text_IO.New_Line; Sum := Sum + CurrentValue; Smallest := Min_Max.Minimum(Value1 => Smallest, Value2 => CurrentValue); Largest := Min_Max.Maximum(Value1 => Largest, Value2 => CurrentValue); END LOOP;

Page 49: Chapter 6 Counting Loops; Subtypes In preceding chapters, 1.Sequence and 2.Conditional structures In this chapter, 3.Repetition, or iteration

-- compute the average; since Sum and NumValues are integers,

-- the fractional part of the average is discarded

Average := Sum / NumValues; -- display the results Ada.Text_IO.Put(Item => "The Smallest is "); Ada.Integer_Text_IO.Put(Item => Smallest, Width => 1); Ada.Text_IO.New_Line; Ada.Text_IO.Put(Item => "The Largest is "); Ada.Integer_Text_IO.Put(Item => Largest, Width => 1); Ada.Text_IO.New_Line; Ada.Text_IO.Put(Item => "The Average is "); Ada.Integer_Text_IO.Put(Item => Average, Width => 1); Ada.Text_IO.New_Line;END Min_Max_Average_File;

Page 50: Chapter 6 Counting Loops; Subtypes In preceding chapters, 1.Sequence and 2.Conditional structures In this chapter, 3.Repetition, or iteration

Payroll ExamplePayroll Example

WITH Ada.Text_IO;WITH Ada.Integer_Text_IO;WITH Ada.Float_Text_IO;PROCEDURE Multi_Payroll IS----------------------------------------------------------

--------| Computes and displays gross pay and net pay for a

number--| of employees, given each employee's hourly rate and --| hours worked.--|-------------------------------------------------------

---------

Page 51: Chapter 6 Counting Loops; Subtypes In preceding chapters, 1.Sequence and 2.Conditional structures In this chapter, 3.Repetition, or iteration

Cont.. PayrollCont.. Payroll

SUBTYPE NonNegFloat IS Float RANGE 0.0 .. Float'Last;

TaxBracket : CONSTANT NonNegFloat := 100.00; -- maximum salary for no tax TaxRate : CONSTANT NonNegFloat := 0.15; -- tax rate

NumEmp : Positive; -- inputs - number of employees Hours : NonNegFloat; -- hours worked, hourly rate HourlyRate: NonNegFloat;

GrossPay: NonNegFloat; -- outputs - gross pay, net pay Tax: NonNegFloat; NetPay: NonNegFloat;

Page 52: Chapter 6 Counting Loops; Subtypes In preceding chapters, 1.Sequence and 2.Conditional structures In this chapter, 3.Repetition, or iteration

Cont.. PayrollCont.. Payroll

BEGIN -- Multi_Payroll Ada.Text_IO.Put ("Please enter number of employees > "); Ada.Integer_Text_IO.Get (Item => NumEmp); FOR CountEmp IN 1 .. NumEmp LOOP -- Enter Hours and HourlyRate Ada.Text_IO.Put (Item => "Employee number "); Ada.Integer_Text_IO.Put (Item => CountEmp, Width => 1); Ada.Text_IO. New_Line; Ada.Text_IO.Put (Item => " Hours worked > "); Ada.Float_Text_IO.Get (Item => Hours); Ada.Text_IO.Put (Item => " Hourly rate $"); Ada.Float_Text_IO.Get (Item => HourlyRate); -- Compute gross salary GrossPay := Hours * HourlyRate;

Page 53: Chapter 6 Counting Loops; Subtypes In preceding chapters, 1.Sequence and 2.Conditional structures In this chapter, 3.Repetition, or iteration

Cont.. PayrollCont.. Payroll

-- Compute net salary IF GrossPay > TaxBracket THEN Tax := TaxRate * (GrossPay - TaxBracket); NetPay := GrossPay - Tax; -- Deduct a tax

amount ELSE NetPay := GrossPay; -- Deduct no tax END IF;

Page 54: Chapter 6 Counting Loops; Subtypes In preceding chapters, 1.Sequence and 2.Conditional structures In this chapter, 3.Repetition, or iteration

Cont.. Payroll exampleCont.. Payroll example-- Display Results Ada.Text_IO.Put (Item => " Gross pay is $"); Ada.Float_Text_IO.Put (Item => GrossPay, Fore => 1, Aft => 2, Exp => 0); Ada.Text_IO.New_Line; Ada.Text_IO.Put (Item => " Tax Deduction is $"); Ada.Float_Text_IO.Put (Item => Tax, Fore => 1, Aft => 2, Exp => 0); Ada.Text_IO.New_Line; Ada.Text_IO.Put (Item => " Net pay is $"); Ada.Float_Text_IO.Put (Item => NetPay, Fore => 1, Aft => 2, Exp => 0); Ada.Text_IO.New_Line;END LOOP;END Multi_Payroll;

Page 55: Chapter 6 Counting Loops; Subtypes In preceding chapters, 1.Sequence and 2.Conditional structures In this chapter, 3.Repetition, or iteration

OverloadingOverloading

Ada allows two or more different Ada allows two or more different operations to have the same name operations to have the same name with enough difference in their with enough difference in their parameter profilesparameter profiles

The same nameThe same name Different parameter profilesDifferent parameter profiles Compiler can distinguish them Compiler can distinguish them

Page 56: Chapter 6 Counting Loops; Subtypes In preceding chapters, 1.Sequence and 2.Conditional structures In this chapter, 3.Repetition, or iteration

AdvantageAdvantage

Operations with similar behavior or Operations with similar behavior or functionality can be given similar functionality can be given similar namesnames

Easier to write and readEasier to write and read

Page 57: Chapter 6 Counting Loops; Subtypes In preceding chapters, 1.Sequence and 2.Conditional structures In this chapter, 3.Repetition, or iteration

HowHow

When compiler reaches When compiler reaches

a procedure or function call a procedure or function call

examine the parameter profileexamine the parameter profile select the appropriate procedureselect the appropriate procedure

Page 58: Chapter 6 Counting Loops; Subtypes In preceding chapters, 1.Sequence and 2.Conditional structures In this chapter, 3.Repetition, or iteration

Example for overloadingExample for overloading

PACKAGE Useful_Functions IS----------------------------------------------------------------------------------------------------------------------------

FUNCTION Minimum (Value1, Value2: Integer) RETURN Integer; FUNCTION Minimum (Value1, Value2: Float) RETURN Float; -- Pre: Value1 and Value2 have been assigned values -- Post: Returns the smaller of the two input values

FUNCTION Maximum (Value1, Value2: Integer) RETURN Integer; FUNCTION Maximum (Value1, Value2: Float) RETURN Float; -- Pre: Value1 and Value2 have been assigned values -- Post: Returns the larger of the two input values

Page 59: Chapter 6 Counting Loops; Subtypes In preceding chapters, 1.Sequence and 2.Conditional structures In this chapter, 3.Repetition, or iteration

Use Overloading WiselyUse Overloading Wisely

Give meaningful namesGive meaningful names Give same name with similar Give same name with similar

functionalityfunctionality Do not abuse by using it too much Do not abuse by using it too much

or by using it to name functions or by using it to name functions and procedures that do not have and procedures that do not have similar behaviorsimilar behavior

Page 60: Chapter 6 Counting Loops; Subtypes In preceding chapters, 1.Sequence and 2.Conditional structures In this chapter, 3.Repetition, or iteration

Cont. exampleCont. example

FUNCTION Sum (N: Positive) RETURN Positive; -- Pre: N has been assigned a value -- Post: Returns the sum of integers from 1 to N

FUNCTION Factorial (N: Positive) RETURN Positive; -- Pre: N has been assigned a value -- Post: Returns the product of integers from 1 to N

END Useful_Functions;

Page 61: Chapter 6 Counting Loops; Subtypes In preceding chapters, 1.Sequence and 2.Conditional structures In this chapter, 3.Repetition, or iteration

Example - Example - body of Useful_Functionsbody of Useful_Functions

FUNCTION Minimum (Value1, Value2: Integer) RETURN Integer IS Result: Integer; BEGIN -- Minimum IF Value1 < Value2 THEN Result := Value1; ELSE Result := Value2; END IF; RETURN Result; END Minimum;

-- minimum of two Float values

Page 62: Chapter 6 Counting Loops; Subtypes In preceding chapters, 1.Sequence and 2.Conditional structures In this chapter, 3.Repetition, or iteration

Cont. exampleCont. example

FUNCTION Minimum (Value1, Value2: Float) RETURN Float IS Result: Float; BEGIN -- Minimum IF Value1 < Value2 THEN Result := Value1; ELSE Result := Value2; END IF; RETURN Result; END Minimum;

Page 63: Chapter 6 Counting Loops; Subtypes In preceding chapters, 1.Sequence and 2.Conditional structures In this chapter, 3.Repetition, or iteration

Example using Example using overlaodingoverlaoding

WITH Ada.Text_IO;WITH Ada.Integer_Text_IO;WITH Ada.Float_Text_IO;WITH Useful_Functions;PROCEDURE Max_Int_Flt IS----------------------------------------------------------------| --------------------------------------------------------------

Int1 : Integer; -- inputs Int2 : Integer; LargerInt : Integer; -- output

Flt1 : Float; -- inputs Flt2 : Float; LargerFlt : Float; -- output

Page 64: Chapter 6 Counting Loops; Subtypes In preceding chapters, 1.Sequence and 2.Conditional structures In this chapter, 3.Repetition, or iteration

BEGIN -- Max_Int_Flt

Ada.Text_IO.Put (Item => "Please enter first integer value > "); Ada.Integer_Text_IO.Get (Item => Int1); Ada.Text_IO.Put (Item => "Please enter second integer value > "); Ada.Integer_Text_IO.Get (Item => Int2);

LargerInt := Useful_Functions.Maximum(Value1=>Int1, Value2=>Int2);

Ada.Text_IO.Put (Item => "The larger integer is "); Ada.Integer_Text_IO.Put (Item => LargerInt, Width => 1); Ada.Text_IO.New_Line;

Ada.Text_IO.Put (Item => "Please enter first float value > "); Ada.Float_Text_IO.Get (Item => Flt1); Ada.Text_IO.Put (Item => "Please enter second float value > "); Ada.Float_Text_IO.Get (Item => Flt2);

LargerFlt := Useful_Functions.Maximum(Value1=>Flt1, Value2=>Flt2);

Ada.Text_IO.Put (Item => "The larger float is "); Ada.Float_Text_IO.Put (Item => LargerFlt, Fore => 1, Aft => 2, Exp => 0); Ada.Text_IO.New_Line;

END Max_Int_Flt;

Page 65: Chapter 6 Counting Loops; Subtypes In preceding chapters, 1.Sequence and 2.Conditional structures In this chapter, 3.Repetition, or iteration

Exceptional HandlingExceptional Handling

Ada allows the programmer to catch Ada allows the programmer to catch the exception before it goes to the the exception before it goes to the Ada run-time systemAda run-time system

out of rangeout of range beginning with a nonnumeric beginning with a nonnumeric

charactercharacter too large numbertoo large number

Page 66: Chapter 6 Counting Loops; Subtypes In preceding chapters, 1.Sequence and 2.Conditional structures In this chapter, 3.Repetition, or iteration

Error Messages and Error Messages and ExceptionException

Constraint_Error ( out of range)Constraint_Error ( out of range) Ada.Text_IO.Data_Error ( bad data)Ada.Text_IO.Data_Error ( bad data)

EXCEPTION

WHEN Constraint_Error => Ada.Text_IO.Put (Item => "The input value or result is out of range."); Ada.Text_IO.New_Line; WHEN Ada.Text_IO.Data_Error => Ada.Text_IO.Put (Item => "The input value is not well

formed."); Ada.Text_IO.New_Line;