Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 7 Other Loop forms, Procedures; Exception Handling.

Similar presentations


Presentation on theme: "Chapter 7 Other Loop forms, Procedures; Exception Handling."— Presentation transcript:

1 Chapter 7 Other Loop forms, Procedures; Exception Handling

2 The General Loop and Exit Problems with FOR statement, when n The loop does not step through all the values of a discrete type in forward or reverse order n The Loop control variable is not discrete n The number of iterations depends on conditions during the execution of the loop

3 Example - Looping when the increment is NOT 1 WITH Ada.Text_IO; WITH Ada.Integer_Text_IO; PROCEDURE Odd_Numbers IS -------------------------------------------------------------- OddNumber : Integer; BEGIN -- Odd_Numbers OddNumber := 1; -- initialize loop LOOP EXIT WHEN OddNumber > 39; -- test for exit Ada.Integer_Text_IO.Put(Item => Oddnumber, Width => 3); OddNumber := Oddnumber + 2; -- update END LOOP; Ada.Text_IO.New_Line; END Odd_Numbers;

4 Example- Looping when increment is not an integer WITH Ada.Text_IO; WITH Ada.Float_Text_IO; PROCEDURE Temperature_Table IS -------------------------------------------------------------- --| Displays a table of Fahrenheit and --| equivalent Celsius temperatures. -------------------------------------------------------------- CStart : CONSTANT Float := 100.0; -- initial Celsius temp CStep : CONSTANT Float := -10.0; -- change in Celsius temp CLimit : CONSTANT Float := -20.0; -- final Celsius temp Celsius : Float; -- Celsius temp Fahrenheit : Float; -- Fahrenheit temp

5 Cont. Example BEGIN -- Temperature_Table Ada.Text_IO.Put(Item => "Celsius Fahrenheit"); Ada.Text_IO.New_Line (Spacing => 2); Celsius := CStart; -- initialize LOOP EXIT WHEN Celsius < CLimit; -- test for exit Fahrenheit := 1.8 * Celsius + 32.0; Ada.Float_Text_IO.Put (Item => Celsius, Fore => 4, Aft => 0, Exp => 0); Ada.Text_IO.Put(Item => " "); Ada.Float_Text_IO.Put (Item => Fahrenheit, Fore => 3, Aft => 1, Exp => 0); Ada.Text_IO.New_Line; Celsius := Celsius + CStep; -- update END LOOP; END Temperature_Table;

6 LOOP Statement LOOP statement sequence; EXIT WHEN condition; statement sequence; END LOOP;

7 Example Looping Controlled by an Event WITH Ada.Text_IO; WITH Ada.Float_Text_IO; PROCEDURE Worm_and_Apple IS -------------------------------------------------------------- --| Displays distances between a worm and an apple. The worm --| keeps reducing the distance by its body length until it is --| close enough to bite the apple. -------------------------------------------------------------- SUBTYPE NonNegFloat IS Float RANGE 0.0.. Float'Last; WormLength: CONSTANT NonNegFloat := 8.5; -- worm body length in CM InitialDist: NonNegFloat; -- input - starting distance -- of worm from apple Distance: NonNegFloat; -- output - diminishing distance -- between worm and apple

8 Cont. Example BEGIN -- Worm_and_Apple Ada.Text_IO.Put (Item => "Initial distance (CM) away from apple > "); Ada.Float_Text_IO.Get(Item => InitialDist); Ada.Text_IO.New_Line; -- Cut the distance between the worm and the apple by -- the worm's body length until the worm is close enough -- to bite the apple

9 Cont. Example Distance := InitialDist; -- initialize LOOP EXIT WHEN Distance <= WormLength; -- test for exit Ada.Text_IO.Put(Item => "The distance is "); Ada.Float_Text_IO.Put (Item => Distance, Fore => 4, Aft => 2, Exp => 0); Ada.Text_IO.New_Line; Distance := Distance - WormLength; -- update END LOOP;

10 Cont. Example -- Display final distance before entering the apple. Ada.Text_IO.New_Line; Ada.Text_IO.Put (Item => "Final distance between worm and apple is "); Ada.Float_Text_IO.Put(Item => Distance, Fore => 4, Aft => 2, Exp => 0); Ada.Text_IO.New_Line; Ada.Text_IO.Put(Item => "The worm bites the apple."); Ada.Text_IO.New_Line; END Worm_and_Apple;

11 FOR I IN 1..5 LOOP Square := i * i; Ada.Integer_Text_IO.Put (Item => i, Width => 1); Ada.Integer_Text_IO.Put (Item => Square, Width => 1); Ada.Text_IO.New_Line; END LOOP; ----------------------------------------------------------------------------------------- i:=1; LOOP EXIT WHEN i > 5; Square := i * i; Ada.Integer_Text_IO.Put (Item => i, Width => 1); Ada.Integer_Text_IO.Put (Item => Square, Width => 1); Ada.Text_IO.New_Line; i:=i +1; END LOOP;

12 In General LOOP n i is needed to declare as a normal variable n i := 1;---- initialization required n i := i +1;---- increment/decrement required n i > 7 ----condition to exit required In FOR LOOP n implicit in the FOR LOOP

13 LOOP DESIGN n Declaration for control variable n (Initialization) for control variable n (Increment/decrement) for control variable n Condition for exit

14 May not need a control variable In Some Problems, for example, n Add numbers n Multiply numbers

15 Flag-Controlled Loop n Initialize Sun to 0 n Initialize MoreData to “Y” n LOOP n EXIT WHEN MoreData = ‘N’ Read the next number to num Add num to Sum Read the next value of MoreData(Y or N) n END LOOP

16 Example- Flag-Controlled Loop Sum := 0; MoreData := ‘Y’; LOOP EXIT WHEN MoreData = ‘N”; Ada.Text_IO.Put(Item => “ Enter the next number ” ); Ada.Integer_Text_IO.Get (Item => num); Sum := Sum + num; Ada.Text_IO.New_Line; Ada.Text_IO.Put(Item => “Any more data Y (Yes) or N (No) ” ); Ada.Text_IO.Get( Item => MoreData); END LOOP;

17 Sentinel-Control Loops n Initialize Sum to 0 n Read the first number into num n Loop Exit WHEN num is the sentinel Add num to Sum Read the next number into num n END LOOP

18 Example - Sentinel-Control Loops Sum := 0; Ada.Text_IO.Put(Item => “ When done, enter -1 to stop ” ); Ada.Text_IO.Put(Item => “ Enter the first number ” ); Ada.Integer_Text_IO.Get (Item => num); Ada.Text_IO.New_Line; LOOP EXIT WHEN Num = Sentinel; Sum := Sum + num; Ada.Text_IO.Put(Item => “ Enter the next number ” ); Ada.Integer_Text_IO.Get (Item => num); Ada.Text_IO.New_Line; END LOOP;

19 Sentinel-Controlled Loop with a Priming Read n Read the first value of input variable n LOOP n EXIT WHEN input variable is equal to sentinel n ---- n Read the next value of input variable n END LOOP

20 Example ---To add positive numbers ---if negative number, exit loop Sum :=0; LOOP Ada.Integer_Text_IO.Get (Item => num); EXIT WHEN num <0; Sum := Sum + num; END LOOP; n

21 Example ---To multiply positive numbers ---if negative number, exit loop Product :=1; LOOP Ada.Integer_Text_IO.Get (Item => num); EXIT WHEN num <0; Product := Product * num; END LOOP

22 Example ---To add positive numbers ---if product is more than 1000, exit loop Product :=1; LOOP Ada.Integer_Text_IO.Get (Item => num); EXIT WHEN product > 1000; Product := Product * num; END LOOP


Download ppt "Chapter 7 Other Loop forms, Procedures; Exception Handling."

Similar presentations


Ads by Google