|
Testing and Debugging Loops
|
|
Prof. David Bernstein
|
| Computer Science Department |
| bernstdh@jmu.edu |
Ensure that you don't compare floating point numbers with the ==
operator
Ensure that you don't change the variable(s) used in the boolean
expression within the body of for loops
Ensure that you do change the variable(s) used in the boolean
expression within the body of while and
do-while loops
Ensure that the correct variable is updated by the correct amount within the body of the loop
Ensure that the loop doesn't terminate early/late (especially "off by one")
Ensure that you don't have a semicolon at the end of for
and/or while loops
Ensure that loop counters are initialized and/or re-initialized properly
for and while loops might
have 0 iterationsi = 0; and n = 10; ensures that the
body is enteredi = 20; and n = 10; ensures that the
body is not entered
print() calls
before while loops to
see if the code will/should enter the bodyprint() calls
(that include the variables that change) in the body
of all loops