< previous page page_492 next page >

Page 492
Enter rainfall amount 11:
0
Enter rainfall amount 12:
0
Average rainfall is 1.29 inches

Do you have another recording site? (y or n) d
Please type y or n: q
Please type y or n: Y
Please type y or n: n
Testing and Debugging
The same testing techniques we used with While loops apply to Do-While and For loops. There are, however, a few additional considerations with these loops.
The body of a Do-While loop always executes at least once. Thus, you should try data sets that show the result of executing a Do-While loop the minimal number of times.
With a data-dependent For loop, it is important to test for proper results when the loop executes zero times. This occurs when the starting value is greater than the ending value (or less than the ending value if the loop control variable is being decremented).
When a program contains a Switch statement, you should test it with enough different data sets to ensure that each branch is selected and executed correctly. You should also test the program with a switch expression whose value is not in any of the case labels.
Testing and Debugging Hints
1. In a Switch statement, make sure there is a break statement at the end of each case alternative. Otherwise, control falls through to the code in the next case alternative.
2. Case labels in a Switch statement are made up of values, not variables. They may, however, include named constants and expressions involving only constants.
3. A switch expression cannot be a floating point expression, and case constants cannot be floating point constants.
4. If there is a possibility that the value of the switch expression might not match one of the case constants, it's best to provide a default alternative.
5. Double-check long Switch statements to make sure that you haven't omitted any branches.

 
< previous page page_492 next page >