Chapter 7
Student Notes
Objectives
The general LOOP and EXIT statements
Exception handling
Procedures vs. Functions
Robust Input Package
Child Packages
1. When a FOR
LOOP won’t work
·
The FOR loop must hit every iteration
·
·
The FOR loop count is, by definition, an integer or enumeration type,
but that is not always the natural loop count.
·
·
Sometimes we want to loop until some condition is met (e.g. x >87)
·
The first two cases are call count-controlled
loops and the last case is called an even-controlled
loop
·
Prog 7.1 p 246 – incrementing is not by 1
·
Prog 7.2 – loop increment not an integer
·
Prog 7.3 – loop controlled by an event
2. general
Statement
sequence1
EXIT WHEN condition;
Statement
sequence2
END
Note the EXIT refers only to
the inner most loop. If a loop is nested inside
another, only the inner loop will be exited, then control is returned to the
outer loop.
3. FOR
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
i := 1;
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
· The statement i := 1 in the general loop is handled implicitly in the FOR LOOP
· The statement i := i +1; is also handled implicitly in the FOR LOOP
· The variable i is a regular variable in the general loop and must be declared at the beginning of the program. In the FOR LOOP is it implicitly declared as an integer
3. Dealing
with an unknown number of iterations
·
Often we don’t know how many times we will need to run the loop before
we write the program
·
Two ways to handle this
·
One:
Sum := 0;
MoreData
:= ‘Y’;
EXIT WHEN MoreData = ‘N’
Ada.Text_IO.Put(Item => “enter the score
> “0;
Ada.Integer_Text_IO.Get(Item => Score);
Ada.Text_IO.New_Line;
Sum := Sum + Score;
Ada.Text_IO.Put(Item => “Any more items? Y
or N?“);
Ada.Text_IO.Get(Item => MoreData);
END
·
Two: sentinel –
·
Sum := 0;
Ada.Text_IO.Put
Item => “When done, enter -1 to stop.”);
Ada.Text_IO.New_Line;
Ada.Text_IO.Put(Item => “enter the first Score > “);
Ada.Integer_Text_IO.Get(Item => Score);
Ada.Text_IO.New_Line;
EXIT WHEN Score = Sentinel
Sum := Sum + Score;
Ada.Text_IO.Put(“Item
=> “Enter next item > “);
Ada.Integer_Text_IO.Get(Item
=> Score);
Ada.Text_IO.New_Line;
END
4. The While
loop
Power
:= 1;
WHILE
Power < 10000 LOOP
Ada.Integer_Text_IO.Put(Item
=> Power, Width => 5);
Power := Power * 2;
END
Power
:=1;
EXIT WHEN Power >= 10000;
Ada.Integer_Text_IO.Put(Item
=> Power, Width => 5);
Power := Power *2;
END
5. Robust
Exception Handling
·
A program is called robust
when it behaves predictably, even when exceptions are raised.
·
·
·
·
Prompt user for an input values
Get the value
EXIT –
valid data
If an exception was raised, notify the user
EXCEPTION
– invalid data
Determine which exception was raised and notify the
user
END
·
If control reaches the EXIT, the data was valid, if the data is
invalid, the exception is handled and the we return to
the top of the loop.
·
Ex prog 7.6 page 270
6. Procedures
vs. Functions
·
Both procedures and functions are subprograms, but they are called in
different ways:
Ada.Float_Text_IO.Put( Item => X, Fore => 3, Aft => 2, Exp =>
0);
Vs.
Temp
:= UsefulFunctions.Minimum (value1 => X, Value2 => Y)
+45;
·
A function can return ,
Procedures return
results
·
Functions have parameters which are only passed
·
Procedures have three different
·
MODE IN parameters:
·
MODE OUT parameters:
·
MODE IN OUT
·
·
·
·
·
·
·
·
E.g. On page 280 gives us an example of some new errors messages
·
When writing procedures be sure that when you call the procedure you
include all the parameters. Each actual parameter must match the type of the
formal parameter. For mode OUT and IN OUT parameters, an actual parameter must
be a variable, for mode IN parameter, and actual parameter may be a variable, a
constant, or an expression
7. Robust
Input Package
·
·
·
·
Page 286, Robust input package
·
You may use Robust Input in your code if it is appropriate
8. A child
package for the Spider package
·
The original Spider package left out two commands that could be very
useful: a TurnLeft and a Step(Howmany) command
·
Page 292 has solutions to these problems
·
·
·
·
·
·
·
We have been using child packages are child packages, they are all
children of the
·
Child packages look exactly like regular packages except for the name: parent.Child
·
Spec for Spider.MyStuff on page 293
·
A child package can see
everything inside the parent package, but a parent
package cannot see into the child package.
· Page 294 for a program using Spider.MyStuff