Week10 DEBUGGING
Week10 DEBUGGING
Week10 DEBUGGING
Debugging
Week 10, Thursday Lab
Debugging Programs
• Manually verify your program first
– Trace the program by hand
• With the different inputs it can take
• Use print statements
• Using the debugger
Off-by-One Loop Errors
• Executing the loop by one more or one less
time than it is supposed to
– If a sentinel-controlled while loop performs extra
repetition, it may erroneously process the sentinel
value along with the regular data
• (e.g. when reading from a file, WHILE eof).
• Checking Loop Boundaries by hand:
– Evaluate initial expression and the final expression
– Substitute these values everywhere the counter
variable appears in the loop body
– Verify that you get the expected results at the
boundaries
Conditional Errors
• Probably the most common error is not using or
misusing BEGIN and END.
• IF <condition> THEN
<statement>
ELSE SINGLE STATEMENTS or
<statement> COMPOUND STATEMENT
– First value of J = K
– Last value of J = N-K
– The assignment at J = K is
Sum := Sqr(K)
– The assignment at J = N-K is
Sum := Prev.Sum + Sqr(N-K)
– At some small value of N=3 & K=1,
trace the loop execution (by hand or with the debugger)
Common Loop Errors:
Too Narrowly Defined Condition
• Example:
WHILE Balance <> 0.0 DO
Update (Balance)
BETTER:
WHILE Balance > 0.0 DO
Update (Balance)
• Don't use inequality for testing conditions,
especially with numbers.
– If Balance goes from negative to positive value without
having 0.0 as a value we will get an infinite loop.
• When using sentinel, make sure that its value can't
be confused with normal data item.
Common Loop Errors: Not
Updating the Loop Control Variable
• If a loop body have more than one statement, don't forget
the BEGIN-END brackets.
–Only REPEAT-UNTIL loop doesn't need a begin-end brackets.
• Example: (infinite loop)