Debugging Logic Errors CPS120 Introduction to Computer Science Lecture 6.

Slides:



Advertisements
Similar presentations
COMPUTER PROGRAMMING I Essential Standard 5.02 Understand Breakpoint, Watch Window, and Try And Catch to Find Errors.
Advertisements

Why care about debugging? How many of you have written a program that worked perfectly the first time? No one (including me!) writes a program that works.
Chapter 3: Editing and Debugging SAS Programs. Some useful tips of using Program Editor Add line number: In the Command Box, type num, enter. Save SAS.
FIT FIT1002 Computer Programming Unit 19 Testing and Debugging.
Basic Control Structures Control order of execution of statements sequential selection iteration - Repeat some action while a certain condition is true.
Lecture Roger Sutton CO331 Visual programming 15: Debugging 1.
Computer Science 1620 Loops.
4 Control Statements.
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Precedence Parentheses Arithemetic ^ * / + - (exception logical not ~ ) Relational > =
The IDE (Integrated Development Environment) provides a DEBUGGER for locating and correcting errors in program logic (logic errors not syntax errors) The.
Finding and Debugging Errors
Visual Basic Debugging Tools Appendix D 6/27/20151Dr. Monther Aldwairi.
Control Structures Control structures control the flow of program execution. 3 types of control structures: sequence, selection.
1 CSE1301 Computer Programming: Lecture 15 Flowcharts, Testing and Debugging.
Computer Programming and Basic Software Engineering 4. Basic Software Engineering 1 Writing a Good Program 4. Basic Software Engineering.
Debugging applications, using properties Jim Warren – COMPSCI 280 S Enterprise Software Development.
Chapter 5: Control Structures II (Repetition)
Outlines Chapter 3 –Chapter 3 – Loops & Revision –Loops while do … while – revision 1.
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
PROGRAMMING IN VISUAL BASIC.NET VISUAL BASIC BUILDING BLOCKS Bilal Munir Mughal 1 Chapter-5.
Lecture 4 Introduction to Programming. if ( grade ==‘A’ ) cout
More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
Chapter 02 (Part III) Introduction to C++ Programming.
Debugging in Java. Common Bugs Compilation or syntactical errors are the first that you will encounter and the easiest to debug They are usually the result.
Testing and Debugging Version 1.0. All kinds of things can go wrong when you are developing a program. The compiler discovers syntax errors in your code.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Program Errors and Debugging Week 10, Thursday Lab.
File Input and Output in C++. Keyboard and Screen I/O #include cin (of type istream) cout (of type ostream) Keyboard Screen executing program input data.
CPS120: Introduction to Computer Science Decision Making in Programs.
Data Structures and Debugging Dr. Nancy Warter-Perez June 18, 2003.
CPS120: Introduction to Computer Science Operations Lecture 9.
Chapter 5 Control Structure (Repetition). Objectives In this chapter, you will: Learn about repetition (looping) control structures Explore how to construct.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
Chapter 5 Loops. Overview u Loop Statement Syntax  Loop Statement Structure: while, for, do-while u Count-Controlled Loops u Nested Loops u Loop Testing.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
Chapter 8 Repetition Statements. Introduction Iteration - process of looping or the repetition of one or more statements Loop body - the statement, or.
Chapter 8 Iteration Dept of Computer Engineering Khon Kaen University.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 8 Debugging, Creating Executable Files, and Distributing a Windows Application.
CS Class 05 Topics  Selection: switch statement Announcements  Read pages 74-83, ,
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
1 EMT 101 – Engineering Programming Dr. Farzad Ismail School of Aerospace Engineering Universiti Sains Malaysia Nibong Tebal Pulau Pinang Week 2.
Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 9 & 10 Repetition Statements.
1 Debugging and Syntax Errors in C++. 2 Debugging – a process of finding and fixing bugs (errors or mistakes) in a computer program.
Debugging Logic Errors CPS120 Introduction to Computer Science.
PROGRAM ESSENTIALS. TOKENS  SMALLEST UNITS OF A PROGRAM LANGUAGE  Special Symbols  Mathematical Operators  Punctuation  Word Symbols  Key Words.
Debugging tools in Flash CIS 126. Debugging Flash provides several tools for testing ActionScript in your SWF files. –The Debugger, lets you find errors.
Chapter 6 - Repetition. while Loop u Simplest loop u Two parts: test expression and loop body u Pre-tested loop –Execute loop body if test true –Bypass.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
The Debugging Process Syntax Errors CPS120 Introduction to Computer Science Lecture 4.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 5: Control Structures II (Repetition)
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
1 Looping Chapter 6 2 Getting Looped in C++ Using flags to control a while statement Trapping for valid input Ending a loop with End Of File condition.
Chapter 4 Repetition Statements Program Development and Design Using C++, Third Edition.
REPETITION CONTROL STRUCTURE
Testing and Debugging.
Chapter 2 Assignment and Interactive Input
Computer Programming I
CSC113: Computer Programming (Theory = 03, Lab = 01)
Outline Altering flow of control Boolean expressions
Introduction to C++ Programming
DEBUGGING JAVA PROGRAMS USING ECLIPSE DEBUGGER
Tonga Institute of Higher Education
Let’s all Repeat Together
An Introduction to Debugging
Chapter 1 c++ structure C++ Input / Output
Presentation transcript:

Debugging Logic Errors CPS120 Introduction to Computer Science Lecture 6

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 Convert 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 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 6. Not using a compound statement when one is required 7. Array index bounds error

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

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

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

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

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

Missing Compound Statement char ch; infile.get (ch); while (infile) cout<< ch; infile.get (ch); Should be { cout << ch; infile.get (ch); }

Array Index Bounds Error const int NUM_DAYS = 7; int temperatures[NUM_DAYS]; int count; for (count =1;count <= NUM_DAYS; count++ { cout << Enter a temperature: “; cin >> temperatures [count]; } This code contains a fundamental error – the array’s correct indices range from 0 through 6, while the array indices used in the for loop range from 1 to 7

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;

Tracing Decisions 1. What to display Variables modified in the block of code 2. 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

ASSERT Macro Stops a program midstream during debugging Cannot resume Cannot examine variables Allows you to stop a program when a specific condition occurs Multiple asserts can be used in the same code assert ( ); If the expression is true (has a non-zero value), the program continues. If false (has a zero value), execution terminates with an error message that the assert failed Requires: #include

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

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

The Step Debugging Process Start out in the Visual C++ editor window Click on BUILD, SET ACTIVE CONFIGURATIONS, WIN 32 DEBUG Click on the BUILD menu, START DEBUG and STEP INTO your program This will start the program but only execute the first line

Debugging View Program Pane – displays source Has current statement indicator Variables Pane – displays list of variables that are defined Lower Left Watch Pane – drag variables and expressions there to see monitor their content Lower Right Call Chain (Stack trace)

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

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

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 verified at 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

Functions There is a pop-up menu for switching functions above the program pane

Arrays, Structs and Classes A + next to a name in the variable pane will show all the values of a structured variable

Preferences Alterable from the TOOLS / OPTIONS menu

Program Testing Sampling: Testing some of the functions of software and making inferences on the rest of the functionality based on these observations Population Testing: Testing every possible option before software is considered complete

Step Debugger One of he bet 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

The Step Debugging Process 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 Start out in the Visual C++ editor window Click on the BUILD menu, START DEBUG and STEP INTO your program This will start the program but only execute the first line

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

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

Watching Variables Use the WATCH window pane in the lower right corner of the debugger screen to check the value of a variable A variable can also be verified at a breakpoint by using QUICKWATCH or you can add it to the WATCH pane

Program Testing Sampling: Testing some of the functions of software and making inferences on the rest of the functionality based on these observations Population Testing: Testing every possible option before software is considered complete