Presentation is loading. Please wait.

Presentation is loading. Please wait.

Debugging Logic Errors CPS120 Introduction to Computer Science.

Similar presentations


Presentation on theme: "Debugging Logic Errors CPS120 Introduction to Computer Science."— 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; misuse of structure and form in programming or a violation of the compiler’s rules. These errors are detected by the compiler A logic error is a mistake that complies with the rules of the compiler that causes the program to generate incorrect output

4 Semantic Errors A semantic error is a violation of the rules of meaning of a programming language E.g. My refrigerator just drove a car to Chicago Overt logic errors Something is obviously wrong even though the program is running Covert logic errors Not so obvious something is wrong Run things various ways to highlight these errors

5 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

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

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

8 Infinite Loop char response; cout ”; cin >> response; while ((response !=‘y’)||(response !=‘n’)) { cout ’;

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

10 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

11 Off-by-one Error A loop iterates one fewer or one more than is correct cont int NUM_VALUES = 50; int lcv,someValue, total=o; for (lcv=1; lcv < NUM_VLUES; lcv++) { cout ”; cin >> someValue; total = total + someValue; }

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

13 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;


Download ppt "Debugging Logic Errors CPS120 Introduction to Computer Science."

Similar presentations


Ads by Google