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

6.10 Tricks of the Trade: Common Programming Errors

Beginners sometimes confuse IF and WHILE statements because both statements contain a condition. Make sure that you use an IF statement to implement a decision step and a WHILE statement to implement a conditional loop. Remember to terminate each control structure with an END IF or END LOOP. The compiler will detect a syntax error if an END IF or END LOOP is missing.

Be careful when using tests for inequality to control the repetition of a WHILE loop. The loop below is intended to process all transactions for a bank account while the balance is positive:

    WHILE Balance /= 0.0 LOOP
      Update (Balance);
    END LOOP;
If the bank balance goes from a positive to a negative amount without being exactly 0.0, the loop will not terminate (an infinite loop). The loop below would be safer:
    WHILE Balance >= 0.0 LOOP
      Update (Balance);
    END LOOP;
Verify that the repetition condition for a WHILE loop will eventually become false. If you use a sentinel-controlled loop, remember to provide a prompt that tells the program user what value to enter as the sentinel. Make sure that the sentinel value cannot be entered as a normal data item.

Keep in mind that exception handlers have to be associated with BEGIN-END blocks, and remember that once a program transfers to an exception handler, control does not automatically return to the statement that caused the exception. If you need to return to that statement (as in the robust input loop), you need to use a LOOP-END LOOP structure to do so.


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

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