debugging logic errors cps120 introduction to computer science
TRANSCRIPT
Debugging Logic Errors
CPS120Introduction 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 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
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
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
Approaches to Correction Desk-checking Inserting Tracing Statements
Used when program "crashes" Runs to completion with incorrect
output Using an interactive debugger
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
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 ->’;
Misunderstanding Operator Precedence
milesPerGallon = endMileage – startMileage /gallonsUsed;
Division has a higher precedence than subtraction
Should be
milesPerGallon = (endMileage –startMileage) / gallonsUsed;
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
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;}
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;
}
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;