Program Errors and Debugging - PowerPoint PPT Presentation

About This Presentation
Title:

Program Errors and Debugging

Description:

Using the debugger. Off-by-One Loop Errors ... The Debugger. Helps you execute your program in a controlled fashion to help you find errors: ... – PowerPoint PPT presentation

Number of Views:21
Avg rating:3.0/5.0
Slides: 17
Provided by: violettaca
Learn more at: http://www.cs.cmu.edu
Category:

less

Transcript and Presenter's Notes

Title: Program Errors and Debugging


1
Program Errors and Debugging
  • Week 10, Thursday Lab

2
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

3
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

4
Conditional Errors
  • Probably the most common error is not using or
    misusing BEGIN and END.
  • IF ltconditiongt THEN ltstatementgtELSE
    ltstatementgt
  • CASE ltselectorgt OF ltlabel-listgt
    ltstatementgt ELSE ltstatement-listgtEND

SINGLE STATEMENTS orCOMPOUND STATEMENT
ltstatementgt ltstatementgt...ltstatementgt
5
Conditional Errors
  • You are not testing all the conditions you should
    be testing
  • Some conditions arent caught and fall through
  • Your condition expressions are not correct (you
    have a logical error)

6
Common Loop ErrorsOff-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 N3 K1, trace the loop
    execution (by hand or with the debugger)

7
Common Loop Errors Too Narrowly Defined
Condition
  • Example
  • WHILE Balance ltgt 0.0 DO
  • Update (Balance)
  • BETTER
  • WHILE Balance gt 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.

8
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 lt 10000 DO
  • writeln('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.

9
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
    doesnt check for you

10
Locating Runtime Errors with PRINT Statements
  • 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
  • Focus on statements in that section to determine
    which are at fault
  • When you locate the error, enclose the diagnostic
    statement with comment braces

11
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

12
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

13
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

14
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

15
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 dont 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

16
Executing with Breakpoints cont
  • Can add a conditional breakpoint
  • If you do, make sure you remove unconditional
    breakpoints at the same place.
  • Breakpoints Delete

17
Evaluating and Modifying Expressions
Write a Comment
User Comments (0)
About PowerShow.com