< previous page page_217 next page >

Page 217
Module Print Activity has five consecutive If statements and a total of eight comparisons. The algorithm performs all five If statements, even if the first condition happens to be satisfied. It would make more sense to code this module as a set of nested If-Then-Elses. With If-Then-Else, we stop testing as soon as a condition is satisfied. The middle Ifs seem to require compound logical conditions with two parts. However, the If-Then-Else structure makes the two-part conditions unnecessary; we don't execute the elsebranch unless one of the parts is already satisfied. Here's the rewritten module:
Print Activity (a better version)
Print The recommended activity is
IF temperature > 85
   Print swimming.
ELSE IF temperature > 70
   Print tennis.
ELSE IF temperature > 32
   Print golf.
ELSE IF temperature > 0
   Print skiing.
ELSE
   Print dancing.

Module Structure Chart:
0217-01.gif
Because this algorithm has just one variable, temperature, we won't bother with the list of variables.
//******************************************************************
// Activity program
// This program outputs an appropriate activity
// for a given temperature
//******************************************************************
#include <iostream.h>

 
< previous page page_217 next page >