Previous | Next | Table of Contents | Index | Program List | Copyright

Chapter Review

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.

New Ada Constructs in Chapter 5

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

Quick-Check Exercises

  1. In the following program fragment, how many times do the 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;
  2. In the following program fragment, how many times do the 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;
  3. In the following program fragment, what values are displayed?
    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);
  4. In the following program, what values are displayed?
    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;

Answers to Quick-Check Exercises

  1. The Put statement executes 50 times; the New_Line executes 10 times; the last value displayed is 50.
  2. The Put statement executes 1 + 2 + 3 + ... + 9 + 10, or 55, times; the New_Line executes 10 times; the last value displayed is 100.
  3. No result is displayed, because the program has a compilation error. The variable Counter cannot be accessed outside of the loop.
  4. The values displayed are 1 2 3 4 5 1. The declared variable Counter is a different variable from the one used to control the loop.

Review Questions

  1. Write a FOR statement that runs from 'Z' to 'A' and displays only the consonants. Hint: Test each character against the vowels.
  2. Write a nested loop that displays the first six letters of the alphabet on a line, the next five letters on the next line, the next four letters on the next line, and so on, down to and including one letter (the letter U) on the last line. Use either uppercase or lowercase letters.
  3. Explain the overloading principle. What examples have you seen of its use?

Programming Projects

  1. Modify Programming Project 1 of Chapter 4 so that ten speeds are handled in a single run. Also, print a count of the number of speeding automobiles.
  2. Write a program that computes the product of a collection of 15 data values. Your program should ignore values that are 0.
  3. Compute and display a table showing the first 15 powers of 2, starting with 20.
  4. Write a program that reads in 20 values and displays the number of values that are positive (greater than or equal to 0) and the number that are negative. Also display "more positive" or "more negative" based on the result.


Previous | Next | Table of Contents | Index | Program List | Copyright

Copyright © 1996 by Addison-Wesley Publishing Company, Inc.