Program Errors and Debugging Week 10, Thursday Lab.

Slides:



Advertisements
Similar presentations
Advanced Programming 15 Feb The “OI” Programming Process Reading the problem statement Thinking Coding + Compiling Testing + Debugging Finalizing.
Advertisements

COMPUTER PROGRAMMING I Essential Standard 5.02 Understand Breakpoint, Watch Window, and Try And Catch to Find Errors.
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.
1 Chapter Five Selection and Repetition. 2 Objectives How to make decisions using the if statement How to make decisions using the if-else statement How.
FIT FIT1002 Computer Programming Unit 19 Testing and Debugging.
BIM313 – Advanced Programming Techniques Debugging 1.
Repeating Actions While and For Loops
James Tam Loops In Python In this section of notes you will learn how to rerun parts of your program without having to duplicate the code.
CS201 - Repetition loops.
J. Michael Moore Other Control Structures CSCE 110 Influenced by material developed by James Tam & Jennifer Welch.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 5 Looping.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 5: Looping by Tony.
James Tam Loops In Pascal In this section of notes you will learn how to rerun parts of your program without having to duplicate the code.
The IDE (Integrated Development Environment) provides a DEBUGGER for locating and correcting errors in program logic (logic errors not syntax errors) The.
1 CS 201 Repetition Debzani Deb. 2 Overview Loops  Endfile-Controlled loop  Nested loop  Do-While loop  Flag-Controlled loop Hand Tracing the code.
Introduction to Computers and Programming More Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
J. Michael Moore Other Control Structures CSCE 110 Influenced by material developed by James Tam & Jennifer Welch.
James Tam Loops In Pascal In this section of notes you will learn how to rerun parts of your program without having to duplicate the code.
CS241 PASCAL I - Control Structures1 PASCAL I - Control Structures Philip Fees CS241.
JavaScript, Fourth Edition
1 Chapter 5 File Objects and Looping Statements (Some slides have been modified from their original format)
Debugging Logic Errors CPS120 Introduction to Computer Science Lecture 6.
REPETITION STRUCTURES. Topics Introduction to Repetition Structures The while Loop: a Condition- Controlled Loop The for Loop: a Count-Controlled Loop.
Fruitful functions. Return values The built-in functions we have used, such as abs, pow, int, max, and range, have produced results. Calling each of these.
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
Unit 4 Repetition and Loops. Key Concepts Flowcharting a loop Types of loops Counter-controlled loops while statement Compound assignment operator for.
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
Programming Logic and Design Fifth Edition, Comprehensive
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.
Chapter 5 Repetition and Loop Statements J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei.
Chapter 5 Repetition and Loop Statements Lecture Notes Prepared By: Blaise W. Liffick, PhD Department of Computer Science Millersville University Millersville,
Chapter 6 Looping CS185/09 - Introduction to Programming Caldwell College.
Chapter 8 Iteration Dept of Computer Engineering Khon Kaen University.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
CS101 Computer Programming I Chapter 4 Extra Examples.
Control Structures Repetition or Iteration or Looping Part II.
Repetition. Control of Flow SEQUENCE SELECTION (if..else, switch…case) REPETITION.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 5: Looping.
Pascal Programming Pascal Loops and Debugging. Pascal Programming Pascal Loops In our first brush with the while do loops, simple comparisons were used.
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
Debugging Logic Errors CPS120 Introduction to Computer Science.
Programming with Loops. When to Use a Loop  Whenever you have a repeated set of actions, you should consider using a loop.  For example, if you have.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
CS241 PASCAL I - Control Structures1 PASCAL Control Structures Modified Slides of Philip Fees.
1 Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
CSC 1010 Programming for All Lecture 4 Loops Some material based on material from Marty Stepp, Instructor, University of Washington.
CS 177 Week 10 Recitation Slides 1 1 Debugging. Announcements 2 2.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Flow of Control: Loops Module 4. Objectives Design a loop Use while, do, and for in a program Use the for-each with enumerations Use assertion checks.
1 Week 9 Loops. 2 Repetition: Loops l Structure: »Usually some initialization code »body of loop »loop termination condition l Several logical organizations.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
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 5: Loops Tarik Booker CS 201 California State University, Los Angeles.
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
Testing and Debugging.
Loop Structures.
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
Testing Key Revision Points.
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
Basic Debugging (compilation)
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Announcements Lab 3 was due today Assignment 2 due next Wednesday
ICS103: Programming in C 5: Repetition and Loop Statements
Presentation transcript:

Program Errors and Debugging Week 10, Thursday Lab

Debugging Programs Manually verify your program first –Trace the program by hand With the different inputs it can take Use print statements Using the debugger

Off-by-One Loop Errors Executing the loop by one more or one less time than it is supposed to –If a sentinel-controlled while loop performs extra repetition, it may erroneously process the sentinel value along with the regular data (e.g. when reading from a file, WHILE eof). Checking Loop Boundaries by hand: –Evaluate initial expression and the final expression –Substitute these values everywhere the counter variable appears in the loop body –Verify that you get the expected results at the boundaries

Conditional Errors Probably the most common error is not using or misusing BEGIN and END. IF THEN ELSE CASE OF: : … ELSE END SINGLE STATEMENTS or COMPOUND STATEMENT ; ;... ;

Conditional Errors You are not testing all the conditions you should be testing –Some conditions aren’t caught and “fall through” Your condition expressions are not correct (you have a logical error…)

Common Loop Errors: Off-by-One Loop Example: Sum of squares of numbers Sum := 0; FOR J := K TO N-K DO Sum := Sum + Sqr(J); –First value of J = K –Last value of J = N-K –The assignment at J = K is Sum := Sqr(K) –The assignment at J = N-K is Sum := Prev.Sum + Sqr(N-K) –At some small value of N=3 & K=1, trace the loop execution (by hand or with the debugger)

Common Loop Errors: Too Narrowly Defined Condition Example: WHILE Balance <> 0.0 DO Update (Balance) BETTER: WHILE Balance > 0.0 DO Update (Balance ) Don't use inequality for testing conditions, especially with numbers. –If Balance goes from negative to positive value without having 0.0 as a value we will get an infinite loop. When using sentinel, make sure that its value can't be confused with normal data item.

Common Loop Errors: Not Updating the Loop Control Variable If a loop body have more than one statement, don't forget the BEGIN-END brackets. –Only REPEAT-UNTIL loop doesn't need a begin-end brackets. Example: (infinite loop) WHILEPower <= DO writeln('Next power of N is ', Power :6); Power := Power * N; Don't use the REPEAT-UNTIL loop if you aren't sure that the loop will have to be executed at least once. In a FOR loop if the starting value is greater (for TO) or smaller than (for DOWNTO) the statement will not execute.

Common Loop Errors Counter variable in a FOR loop should not be changed inside the loop body. –Example: what happens if you do (RECTBAD1.PAS) It is illegal to use the same counter variable in two nested FOR loops. –Example: what happens if you do (RECTBAD2.PAS) Unfortunately the Turbo Pascal environment doesn’t check for you

Locating Runtime Errors with ‘PRINT’ Statements 1.Examine program output to determine which part of the program is generating incorrect results –Insert extra debugging statements (‘PRINT’ statements) to display intermediate results at different points in your program –HOW? Insert extra writeln statements to trace the values of certain critical variables during program execution –In order to not add any problems be careful that you insert extra writeln statement check if you need to add BEGIN-END brackets 2.Focus on statements in that section to determine which are at fault 3.When you locate the error, enclose the diagnostic statement with comment braces

The Debugger Helps you execute your program in a controlled fashion to help you find errors: –Stepping through program –Seeing output –Watching the value of variables –Running from breakpoint to breakpoint Prints diagnostics when run-time error occurs Indicates the statement that caused the error and displays the values of the selected variables

Stepped Program Execution Run Menu commands: –F7: Trace into2 –F8: Step over –F4: Go to cursor –Run: to through run to the end Usually used in conjunction with output or variable watching NOTE: F7 and F8 are hard to distinguish sometimes

Watching Output During Execution Output command (Debug Menu): –Opens output window at same time as program window –Allows seeing program running with tracing commands (F4, F7, F8) –Shows output as control moves through program

Watching Variables & Expressions Watch: –Can select several values whose values will automatically be displayed after each statement execution. –Can type in an expression to be evaluated based on variables in program

Executing with Breakpoints Causes the program to stop at selected statements You can set several breakpoints at a program. The program will execute directly from one breakpoint to the next Good for large programs (when don’t want to use F7, F8 or even F4). Signaled in Red/Brown – then in blue-green when you get there. Run from breakpoint to breakpoint

Executing with Breakpoints [cont] Can add a conditional breakpoint If you do, make sure you remove unconditional breakpoints at the same place. –Breakpoints : Delete

Evaluating and Modifying Expressions