1 / 18

Debugging Logic Errors

Debugging Logic Errors. CPS120 Introduction to Computer Science. Compiling and Debugging. Executable code will not be created until you correct all of the syntax errors in your source code Then the fun (with logic errors ) begins. Syntax & Logic Errors.

Download Presentation

Debugging Logic Errors

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Debugging Logic Errors CPS120 Introduction to Computer Science

  2. Compiling and Debugging • Executable code will not be created until you correct all of the syntax errors in your source code • Then the fun (with logic errors) begins

  3. Syntax & Logic Errors • A syntax error is simply the violation of the rules of a language • These errors are detected by the compiler • Alogic erroris a mistake that complies with the rules of the compiler that causes the program to generate incorrect output

  4. It Did What?? • If the data is good and a program does not do what it is supposed to do, there must be at least one logic error present • The syntax rules of C++ have been correctly followed, but the meaning of the code is incorrect

  5. Approaches to Correction • Desk-checking • Inserting Tracing Statements • Used when program "crashes" • Runs to completion with incorrect output • Using an interactive debugger

  6. Common Semantic Errors • Infinite Loop • Created when a loop in which the expression tested never becomes false • Misunderstanding operator precedence • Dangling else • Off-By-One Error • Loop that iterates one fewer or one more than is correct • Code inside a loop that doesn’t belong there

  7. Infinite Loop char response; cout << “Please enter (y)es or (n)o ->”; cin >> response; while ((response !=‘y’)||(response !=‘n’)) { cout << “Please try again. Enter (y)es or (n)o ->’;

  8. Misunderstanding Operator Precedence milesPerGallon = endMileage – startMileage /gallonsUsed; • Division has a higher precedence than subtraction Should be milesPerGallon = (endMileage –startMileage) / gallonsUsed;

  9. Dangling ELSE If (relative) if (my friend) cout << “both“; else cout << “neither”; • When this code is run, it prints “both” correctly when both bool variables are true • If both are false, nothing prints • If relative is true but friend is false, it prints neither

  10. Off-by-one Error • A loop iterates one fewer or one more than is correct const int NUM_VALUES = 50; int lcv, someValue, total=0; for (lcv=1; lcv < NUM_VALUES; lcv++) { cout << “Enter an integer ->”; cin >> someValue; total = total + someValue; }

  11. Bad Code Inside a Loop for (lcv = 1; lcv <= Num_VALUES; lcv++) { cout <<“Enter an integer ->”; cin >> someValue; total = total + somevalue; average = total / NUM_VALUES; cout << "Total is: " << total << endl; cout << " Average is: " << average; }

  12. Tracing Statements • Inserting print statements to display the current status of critical variables, i.e. those being modified //Debug trace statement cout << endl << "Location 1:" << endl << "Highest values is:" << highest_value << "Lowest value is:" << lowest_value << endl << "Sum of positives is:" << sum_pos_values << "Sum of negatives is:" << sum_neg_values << endl << "Mean of positives is:" << mean_pos_values << "Mean of negatives is:" << mean_neg_values << endl << endl;

  13. Tracing Decisions • What to display • Variables modified in the block of code • Where to place the displays • At logical breakpoints • Before and after IF…ELSE • Before and after LOOPS and when counters are incremented • At the end of 15 or 20 sequential statements

  14. Interactive (Step) Debugger • One of he best tools for uncovering logic errors and for testing to make sure the problems are non-existent is the step debugger in the Visual C++ programming environment • Allows programmers to execute their code one line at a time • Allows you to look at the values of variables during the pause • Will not help with syntax problems, since it requires an executable to work

  15. Interactive Debugging • Discover critical points by stopping execution and taking a snapshot of the program's activity • Resume activity • Check values of critical variables • Observe the 'flow of control ' • Run one statement at a time or in groups • Order of execution • Dangling ELSEs, missing subroutine calls, infinite loops

  16. Stepping Options • In the DEBUG window, you have several options: • GO (F5): Use this option when you have set some breakpoints in the program and you want to get to them or between them quickly • STEP COMMANDS: Executes one statement. • STEP INTO (F11): Tells the program to enter a function or other command instead of just processing • STEP OVER (F10): Won’t keep a line from executing, just steps over it • STEP OUT (Shift-F11): Returns control to the portion of the code that called a function • RUN TO CURSOR (CNTL-F10): Will run the program to wherever the programmer places the cursor in a program

  17. Inserting & Removing Breakpoints • Before you use the step debugger, you may want to review the source code and try to determine the general area where errors are occurring. This is done by setting breakpoints • This allows you to get to interesting sections of code • Breakpoints have no impact on your program, other than how it executes • Click on the "hand" (breakpoint) button to set or remove breakpoints • Breaks must be in executable statements

  18. Watching Variables • Use the WATCH window pane in the lower right corner of the debugger screen to check the value of a variable • Right click and check properties to see variable types • A variable can also be verifiedat a breakpoint by using QUICKWATCH or you can add it to the WATCH pane • Change the value of a variable by double-clicking on it in the variables pane

More Related