debugging logic errors cps120 introduction to computer science

13
Debugging Logic Errors CPS120 Introduction to Computer Science

Upload: rebecca-nelson

Post on 18-Jan-2016

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Debugging Logic Errors CPS120 Introduction to Computer Science

Debugging Logic Errors

CPS120Introduction to Computer Science

Page 2: 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

Page 3: Debugging Logic Errors CPS120 Introduction to Computer Science

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

Page 4: Debugging Logic Errors CPS120 Introduction to Computer Science

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

Page 5: Debugging Logic Errors CPS120 Introduction to Computer Science

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

Page 6: Debugging Logic Errors CPS120 Introduction to Computer Science

Approaches to Correction Desk-checking Inserting Tracing Statements

Used when program "crashes" Runs to completion with incorrect

output Using an interactive debugger

Page 7: Debugging Logic Errors CPS120 Introduction to Computer Science

Common Semantic Errors1. Infinite Loop

Created when a loop in which the expression tested never becomes false

2. Misunderstanding operator precedence3. Dangling else4. 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

Page 8: Debugging Logic Errors CPS120 Introduction to Computer Science

Infinite Loopchar 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 ->’;

Page 9: Debugging Logic Errors CPS120 Introduction to Computer Science

Misunderstanding Operator Precedence

milesPerGallon = endMileage – startMileage /gallonsUsed;

Division has a higher precedence than subtraction

Should be

milesPerGallon = (endMileage –startMileage) / gallonsUsed;

Page 10: Debugging Logic Errors CPS120 Introduction to Computer Science

Dangling ELSEIf (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

Page 11: Debugging Logic Errors CPS120 Introduction to Computer Science

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 << “Enter an integer ->”;cin >> someValue;total = total + someValue;}

Page 12: Debugging Logic Errors CPS120 Introduction to Computer Science

Bad Code Inside a Loopfor (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;

}

Page 13: Debugging Logic Errors CPS120 Introduction to Computer Science

Tracing Statements Inserting print statements to display the

current status of critical variables, i.e. those being modified

//Debug trace statementcout << 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;