Instructor's Manual for

M.B. Feldman and E.B. Koffman, Ada 95 Problem Solving and Program Design, 3rd edition. Copyright 1999, Addison-Wesley Publishing Company. All Rights Reserved

Questions and comments to mfeldman@seas.gwu.edu


Chapter 6 Counting Loops; Subtypes

last revised March 1999

Chapter Objectives

The student will

  1. be introduced to repetition in programs
  2. learn the syntax and semantics of the FOR statement
  3. learn how to use FOR loops to accumulate sums and products
  4. learn how to use FOR loops to display tables of information
  5. learn how to use nested FOR loops
  6. learn about scalar subtypes
  7. be introduced to the overloading principle
  8. be introduced to exception handling

New Terms

Section 6.1

FOR statement

counter-controlled loop

Section 6.2

first and last attributes

Section 6.3

external file

Section 6.5

nested loops

 

Section 6.6

enumeration subtypes

Section 6.7

debugger program

 regression testing

Section 6.8

overloading

factorial algorithm

Section 6.9

exception handler

robust program

Notes and Suggestions

General Suggestions

FOR loops are generally simpler and more intuitive than WHILE loops, and therefore we have introduced FOR loops first. Indeed, the limitations of FOR loops in general, and Ada's in particular, serve as good motivation for the WHILE and general loop statements introduced in Chapter 6.

Section 6.1

Ada's FOR statement is somewhat different from its counterpart in other languages. Not only is it impermissible, as in Pascal, to modify the counter variable in the loop body, but the counter variable is declared implicitly and has no existence outside the loop body. Indeed, a counter variable will hide an explicit declaration of a variable with the same name and type. You might show students an example of this.

Also explain that in Ada, a FOR loop does not have a "step" parameter, but simply steps forward or backward through a range, one value at a time. If an explicit step size is required, a WHILE or general loop must be used.

Beginning students might not understand intuitively how to accumulate a sum or product; explain carefully how a statement like Sum := Sum + Item works.

Section 6.2

The attributes Integer'First and Integer'Last are introduced in this section. Pascal programmers can see these as equivalent to Maxint and &endash;Maxint, but Ada attributes are much more general, because they are available for all scalar types. Explain why attributes are better than "magic numbers" for initializing variables to hold the largest and smallest values.

Section 6.3

A bit of file handling is introduced in this section. Explain the association that an Open statement sets up between a program variable and an operating-system file name. I have sometimes introduced input redirection here, as an alternative to opening a file explicitly. Because redirection is system-specific, it is not introduced in the text until Chapter 10 in the sytematic discussion of files. Students usually do see the advantage in preparing an eternal file of data that they can use in repeatedly testing their programs.

Section 6.4

The multi-employee payroll program illustrates the role of repetition in writing powerful programs: in this example, an entire section of code is repeated, and the number of repetitions is not known until execution time. This is a good example of a program altering its behavior in response to the input data.

Section 6.5

Emphasize to students that there is simply no substitute for extensive tracing in building understanding of nested loops.

I have used a classroom dramatization to reinforce nested loops. Recruit three volunteer students to come to the front (I have sometimes enlisted three "wise-guy" types for this). Give Student 1 a slip of paper saying

Whenever Student 2 says "do it", do the following:

Turn around 360 degrees

Student 2's paper says

Whenever Student 3 says "do it", do the following:

Repeat 5 times: say "do it" to Student 1; wait till he's done it to instruct him again

Student 3's paper says

Repeat 6 times: say "do it" to Student 2; wait till he's done it to instruct him again

Soon the class gets the idea that Student 1 will get tired of spinning around, and everyone has had a good time.

A good project at this point is to produce an output file containing a "daily calendar" listing, one line for each day in a given range of months, e.g., March 1996 through June 1999. I have used this with success, using a lecture hour to develop the algorithm by successive refinement.

Section 6.6

Point out that unfortunately a subtype must specify a contiguous range; there is no way to specify a subtype of Days that include Mon, Wed, and Fri.

Section 6.7

This might be a good opportunity to introduce students to the class compiler's debugger facilities; an on-line demonstration of the debugger is often very helpful.

Section 6.8

Overloading will be new to nearly everyone, and will surprise students with experience in most other languages. Explain that the various Put operations in the I/O libraries are an example of overloading, and that the compiler resolves a subprogram-call by examining the parameter profile to find a matching subprogram definition. If a unique match is found, all is well. If two are found, the overload cannot be resolved; if none is found, the programmer may have misspelled the subprogram's name.

It is also worth discussing that even simple arithmetic involves a less visible form of overloading: the arithmetic operators are different for Float arguments than for Integer ones, and produce different machine code, even though these operators look identical.

Section 6.9

Exception handling is the least familiar Ada feature thus far in the book. Explain that an exception handler, if present at the end of a program, is never "flowed into" but executed only if an exception is raised. You might describe a program as having a "normal processing" path and an "abnormal processing" path which is taken only if the execution of some statement is in error.

Students will probably want to know how they can correct an error and re-try the statement that failed. This subject is taken up in detail in Chapter 7.

Often students with experience in other languages characterize exceptions as useless baggage in a language, asserting that they can accomplish the same thing with flags and a lot of IF statements. This is, of course, true: exceptions can be seen as well-structured, well-defined error flags. Used properly and in moderation, exception handlers can make normal vs. abnormal flow very obvious to the reader, and also save the programmer the trouble of inventing a new error-flag system for each application.