|
|
|
|
|
|
|
This assertion is the conjunction of four individual assertions. The conjunction of the second and fourth assertions |
|
|
|
|
|
|
|
|
1 <= number <= 11 AND number > 10 |
|
|
|
|
|
|
|
|
implies that number must equal 11. Thus, our four assertions are reduced to three: |
|
|
|
|
|
|
|
|
sum contains the sum of the integers from 0 through number - 1 AND number = 11 AND The number of loop iterations executed equals number - 1. |
|
|
|
|
|
|
|
|
If all three of these are true, we can substitute the value 11 for number in the first and third assertions, giving us the desired postcondition: |
|
|
|
|
|
|
|
|
sum contains the sum of the integers from 0 through 10 AND number = 11 AND The number of loop iterations executed equals 10. |
|
|
|
|
|
|
|
|
This may seem like an awful lot of work to show something that's obvious anyway: The code is correct. But what is obvious in simple code may not be obvious in more complicated code. That's where verification methods can really help. |
|
|
|
|
|
|
|
|
Testing and Debugging Hints |
|
|
|
|
|
|
|
|
1. Plan your test data carefully to test all sections of a program. |
|
|
|
|
|
|
|
|
2. Beware of infinite loops, where the expression in the While statement never becomes FALSE. The symptom: The program doesn't stop. If you are on a system that monitors the execution time of a program, you may see a message like TIME LIMIT EXCEEDED. |
|
|
|
|
|
|
|
|
If you have created an infinite loop, check your logic and the syntax of your loops. Be sure there's no semicolon immediately after the right parenthesis of the While condition: |
|
|
|
 |
|
|
|
|
while (Expression);
Statement |
|
|
|
|
|
|
|
|
This causes an infinite loop in most cases; the compiler thinks the loop body is the null statement (the do-nothing statement terminated by a semicolon). In a count-controlled loop, make sure the loop control vari- |
|
|
|
|
|