We showed how to implement repetition in Ada using the counting loop or
FOR
statement.
Algorithm and program traces are used to verify that an algorithm or program is correct. Errors in logic can be discovered by carefully tracing an algorithm or program. Tracing an algorithm or program before entering the program in the computer will save you time in the long run.
We also introduced the important concept of subtypes. Subtypes are used both to improve program readability and to enable the detection of out-of-range values. The operators that can be used with a subtype are the same as for its base type.
We also discussed the issue of type compatiblity. A subtype is compatible with its base type and with all other subtypes of the same base type. This means that an operator can have one operand whose type is the subtype and one operand whose type is the base type, or indeed another subtype.
Another important concept introduced in this chapter was overloading,
which in Ada permits several functions or procedures to be given the same name,
as long as they have different parameter profiles. This is convenient for
giving names to operations like Minimum
, which have similar
function regardless of the type on which they operate.
Finally, exception handling was discussed. Exception handling is Ada's way of allowing a program to keep control even in the event of an error.
FOR
statement:
FOR CurMonth IN March..July LOOP The loop body is repeated for Ada.Float_Text_IO.Get(Item=>MonthSales); each value of CurMonth from YearSales := YearSales+MonthSales; March through July, inclusive. END LOOP; For each month, the value of MonthSales is read and added to YearSales.Subtype definition:
SUBTYPE FDIC_Insured IS Float RANGE 0.0..100000.0; declares a subtype of Float in the range 0.0-100000.0
Put
and New_Line
statements execute? What is the last value displayed?
FOR I IN 1..10 LOOP FOR J IN 1..5 LOOP Ada.Integer_Text_IO.Put(Item => I * J, Width => 5); END LOOP; Ada.Text_IO.New_Line; END LOOP;
Put
and New_Line
statements execute? What is the last value
displayed?
FOR I IN 1..10 LOOP FOR J IN 1..I LOOP Ada.Integer_Text_IO.Put(Item => I * J, Width => 5); END LOOP; Ada.Text_IO.New_Line; END LOOP;
FOR Counter IN 1..5 LOOP Ada.Integer_Text_IO.Put(Item => Counter, Width => 5); END LOOP; Ada.Integer_Text_IO.Put(Item => Counter, Width => 5);
WITH Ada.Integer_Text_IO; PROCEDURE TryIt IS Counter: Integer; BEGIN -- TryIt Counter := 1; FOR Counter IN 1..5 LOOP Ada.Integer_Text_IO.Put(Item => Counter, Width => 5); END LOOP; Ada.Integer_Text_IO.Put(Item => Counter, Width => 5); END TryIt;
Put
statement executes 50 times; the
New_Line
executes 10 times; the last value displayed is 50.
Put
statement executes 1 + 2 + 3 + ... + 9 + 10, or 55,
times; the New_Line
executes 10 times; the last value displayed is
100.
Counter
cannot be accessed outside of the loop.
Counter
is a different variable from the one used to control the
loop.FOR
statement that runs from 'Z'
to
'A'
and displays only the consonants. Hint: Test each character
against the vowels.
U
) on the last line. Use either uppercase or lowercase letters.
"more positive"
or "more
negative"
based on the result.
Copyright © 1996 by Addison-Wesley Publishing Company, Inc.