16
Program Errors and Debugging Week 10, Thursday Lab

Program Errors and Debugging Week 10, Thursday Lab

Embed Size (px)

Citation preview

Page 1: Program Errors and Debugging Week 10, Thursday Lab

Program Errors and Debugging

Week 10, Thursday Lab

Page 2: Program Errors and 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

Page 3: Program Errors and Debugging Week 10, Thursday Lab

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

Page 4: Program Errors and Debugging Week 10, Thursday Lab

Conditional Errors

• Probably the most common error is not using or misusing BEGIN and END.

• IF <condition> THEN <statement>ELSE <statement>

• CASE <selector> OF: <label-list> : <statement> …ELSE <statement-list>END

SINGLE STATEMENTS orCOMPOUND STATEMENT

<statement> ;<statement>;...<statement>;

Page 5: Program Errors and Debugging Week 10, Thursday Lab

Conditional Errors

• You are not testing all the conditions you should be testing– Some conditions aren’t caught and “fall

through”

• Your condition expressions are not correct (you have a logical error…)

Page 6: Program Errors and Debugging Week 10, Thursday Lab

Common Loop Errors:Off-by-One Loop

• Example: Sum of squares of numbers Sum := 0;FOR J := K TO N-K DO

Sum := Sum + Sqr(J);

– 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)

Page 7: Program Errors and Debugging Week 10, Thursday Lab

Common Loop Errors: Too Narrowly Defined Condition

• Example:WHILE Balance <> 0.0 DO

Update (Balance)BETTER:

WHILE Balance > 0.0 DOUpdate (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.

Page 8: Program Errors and Debugging Week 10, Thursday Lab

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)

WHILE Power <= 10000 DOwriteln('Next power of N is ', Power :6);Power := Power * N;

• Don't use the REPEAT-UNTIL loop if you aren't sure that the loop will have to be executed at least once.

• In a FOR loop if the starting value is greater (for TO) or smaller than (for DOWNTO) the statement will not execute.

Page 9: Program Errors and Debugging Week 10, Thursday Lab

Common Loop Errors

• Counter variable in a FOR loop should not be changed inside the loop body.– Example: what happens if you do

(RECTBAD1.PAS)

• It is illegal to use the same counter variable in two nested FOR loops.– Example: what happens if you do

(RECTBAD2.PAS)

• Unfortunately the Turbo Pascal environment doesn’t check for you

Page 10: Program Errors and Debugging Week 10, Thursday Lab

Locating Runtime Errors with ‘PRINT’ Statements

1. Examine program output to determine which part of the program is generating incorrect results– Insert extra debugging statements (‘PRINT’ statements) to

display intermediate results at different points in your program

– HOW? Insert extra writeln statements to trace the values of certain critical variables during program execution

– In order to not add any problems be careful that you insert extra writeln statement check if you need to add BEGIN-END brackets

2. Focus on statements in that section to determine which are at fault

3. When you locate the error, enclose the diagnostic statement with comment braces

Page 11: Program Errors and Debugging Week 10, Thursday Lab

The Debugger

• Helps you execute your program in a controlled fashion to help you find errors:– Stepping through program– Seeing output– Watching the value of variables– Running from breakpoint to breakpoint

• Prints diagnostics when run-time error occurs• Indicates the statement that caused the error

and displays the values of the selected variables

Page 12: Program Errors and Debugging Week 10, Thursday Lab

Stepped Program Execution

• Run Menu commands:– F7: Trace into2– F8: Step over– F4: Go to cursor– Run: to through run to the end

• Usually used in conjunction with output or variable watching

• NOTE: F7 and F8 are hard to distinguish sometimes

Page 13: Program Errors and Debugging Week 10, Thursday Lab

Watching Output During Execution

• Output command (Debug Menu):– Opens output window at same time as

program window– Allows seeing program running with tracing

commands (F4, F7, F8)– Shows output as control moves through

program

Page 14: Program Errors and Debugging Week 10, Thursday Lab

Watching Variables & Expressions

• Watch:– Can select several values whose values will

automatically be displayed after each statement execution.

– Can type in an expression to be evaluated based on variables in program

Page 15: Program Errors and Debugging Week 10, Thursday Lab

Executing with Breakpoints

• Causes the program to stop at selected statements

• You can set several breakpoints at a program. The program will execute directly from one breakpoint to the next

• Good for large programs (when don’t want to use F7, F8 or even F4).

• Signaled in Red/Brown – then in blue-green when you get there.

• Run from breakpoint to breakpoint

Page 16: Program Errors and Debugging Week 10, Thursday Lab

Executing with Breakpoints [cont]

• Can add a conditional breakpoint

• If you do, make sure you remove unconditional breakpoints at the same place.– Breakpoints : Delete