Title: Other Loop forms, Procedures Exception Handling
1Chapter 7
- Other Loop forms, Procedures Exception Handling
2The General Loop and Exit
- Problems with FOR statement, when
- The loop does not step through all the values of
a discrete type in forward or reverse order - The Loop control variable is not discrete
- The number of iterations depends on conditions
during the execution of the loop
3Example - 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 gt 39 -- test for
exit - Ada.Integer_Text_IO.Put(Item gt Oddnumber,
Width gt 3) - OddNumber Oddnumber 2 -- update
- END LOOP
- Ada.Text_IO.New_Line
- END Odd_Numbers
4Example- 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
5Cont. Example
- BEGIN -- Temperature_Table
- Ada.Text_IO.Put(Item gt "Celsius
Fahrenheit") - Ada.Text_IO.New_Line (Spacing gt 2)
- Celsius CStart -- initialize
- LOOP
- EXIT WHEN Celsius lt CLimit -- test for
exit - Fahrenheit 1.8 Celsius 32.0
- Ada.Float_Text_IO.Put
- (Item gt Celsius, Fore gt 4, Aft gt 0, Exp
gt 0) - Ada.Text_IO.Put(Item gt " ")
- Ada.Float_Text_IO.Put
- (Item gt Fahrenheit, Fore gt 3, Aft gt 1,
Exp gt 0) - Ada.Text_IO.New_Line
- Celsius Celsius CStep -- update
- END LOOP
- END Temperature_Table
6LOOP Statement
- LOOP
- statement sequence
- EXIT WHEN condition
- statement sequence
- END LOOP
7ExampleLooping 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
8Cont. Example
- BEGIN -- Worm_and_Apple
- Ada.Text_IO.Put (Item gt "Initial distance (CM)
away from apple gt ") - Ada.Float_Text_IO.Get(Item gt 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
-
9Cont. Example
- Distance InitialDist --
initialize - LOOP
- EXIT WHEN Distance lt WormLength -- test
for exit - Ada.Text_IO.Put(Item gt "The distance is ")
- Ada.Float_Text_IO.Put
- (Item gt Distance, Fore gt 4, Aft gt 2, Exp
gt 0) - Ada.Text_IO.New_Line
- Distance Distance - WormLength -- update
- END LOOP
-
10Cont. Example
- -- Display final distance before entering the
apple. - Ada.Text_IO.New_Line
- Ada.Text_IO.Put (Item gt "Final distance
between worm and apple is ") - Ada.Float_Text_IO.Put(Item gt Distance, Fore gt
4, Aft gt 2, Exp gt 0) - Ada.Text_IO.New_Line
- Ada.Text_IO.Put(Item gt "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 gt i, Width gt
1) - Ada.Integer_Text_IO.Put (Item gt Square,
Width gt 1) - Ada.Text_IO.New_Line
- END LOOP
- --------------------------------------------------
--------------------------------------- - i1
- LOOP
- EXIT WHEN i gt 5
- Square i i
- Ada.Integer_Text_IO.Put (Item gt i, Width gt
1) - Ada.Integer_Text_IO.Put (Item gt Square,
Width gt 1) - Ada.Text_IO.New_Line
- ii 1
- END LOOP
12- In General LOOP
- i is needed to declare as a normal variable
- i 1 ---- initialization required
- i i 1 ---- increment/decrement required
- i gt 7 ----condition to exit required
- In FOR LOOP
- implicit in the FOR LOOP
13LOOP DESIGN
- Declaration for control variable
- (Initialization) for control variable
- (Increment/decrement) for control variable
- Condition for exit
14May not need a control variable
- In Some Problems,
- for example,
- Add numbers
- Multiply numbers
15Flag-Controlled Loop
- Initialize Sun to 0
- Initialize MoreData to Y
- LOOP
- EXIT WHEN MoreData N
- Read the next number to num
- Add num to Sum
- Read the next value of MoreData(Y or N)
- END LOOP
16Example-Flag-Controlled Loop
- Sum 0
- MoreData Y
- LOOP
- EXIT WHEN MoreData N
- Ada.Text_IO.Put(Item gt Enter the next
number ) - Ada.Integer_Text_IO.Get (Item gt num)
- Sum Sum num
- Ada.Text_IO.New_Line
- Ada.Text_IO.Put(Item gt Any more data Y
(Yes) or N (No) ) - Ada.Text_IO.Get( Item gt MoreData)
- END LOOP
-
17Sentinel-Control Loops
- Initialize Sum to 0
- Read the first number into num
- Loop
- Exit WHEN num is the sentinel
- Add num to Sum
- Read the next number into num
- END LOOP
18Example -Sentinel-Control Loops
- Sum 0
- Ada.Text_IO.Put(Item gt When done, enter -1 to
stop ) - Ada.Text_IO.Put(Item gt Enter the first number
) - Ada.Integer_Text_IO.Get (Item gt num)
- Ada.Text_IO.New_Line
- LOOP
- EXIT WHEN Num Sentinel
- Sum Sum num
- Ada.Text_IO.Put(Item gt Enter the next
number ) - Ada.Integer_Text_IO.Get (Item gt num)
- Ada.Text_IO.New_Line
- END LOOP
19Sentinel-Controlled Loop with a Priming Read
- Read the first value of input variable
- LOOP
- EXIT WHEN input variable is equal to sentinel
- ----
- ----
- Read the next value of input variable
- END LOOP
20Example ---To add positive numbers---if
negative number, exit loop
- Sum 0
- LOOP
- Ada.Integer_Text_IO.Get (Item gt num)
- EXIT WHEN num lt0
- Sum Sum num
- END LOOP
-
21Example ---To multiply positive numbers---if
negative number, exit loop
- Product 1
- LOOP
- Ada.Integer_Text_IO.Get (Item gt num)
- EXIT WHEN num lt0
- Product Product num
- END LOOP
22Example ---To add positive numbers---if product
is more than 1000, exit loop
- Product 1
- LOOP
- Ada.Integer_Text_IO.Get (Item gt num)
- EXIT WHEN product gt 1000
- Product Product num
- END LOOP