ME 142 Engineering Computation Iemp.byui.edu/MILLERG/ME142/SM/Unit 5.3 In Class - Debugging.pdf ·...
Transcript of ME 142 Engineering Computation Iemp.byui.edu/MILLERG/ME142/SM/Unit 5.3 In Class - Debugging.pdf ·...
![Page 1: ME 142 Engineering Computation Iemp.byui.edu/MILLERG/ME142/SM/Unit 5.3 In Class - Debugging.pdf · Testing Center Portion –11/12-11/15 Final Project Groups of 3-4 individuals Topic:](https://reader036.fdocuments.us/reader036/viewer/2022062918/5ffd185c6fde7e293b75d277/html5/thumbnails/1.jpg)
ME 142Engineering Computation I
Debugging Techniques
![Page 2: ME 142 Engineering Computation Iemp.byui.edu/MILLERG/ME142/SM/Unit 5.3 In Class - Debugging.pdf · Testing Center Portion –11/12-11/15 Final Project Groups of 3-4 individuals Topic:](https://reader036.fdocuments.us/reader036/viewer/2022062918/5ffd185c6fde7e293b75d277/html5/thumbnails/2.jpg)
Important Upcoming Events
VBA Exam – 2 parts
In Class Portion – Wednesday 11/14
Testing Center Portion – 11/12-11/15
Final Project
Groups of 3-4 individuals
Topic: Projectile Motion with Air Resistance
![Page 3: ME 142 Engineering Computation Iemp.byui.edu/MILLERG/ME142/SM/Unit 5.3 In Class - Debugging.pdf · Testing Center Portion –11/12-11/15 Final Project Groups of 3-4 individuals Topic:](https://reader036.fdocuments.us/reader036/viewer/2022062918/5ffd185c6fde7e293b75d277/html5/thumbnails/3.jpg)
HWO Submission Reminder
Don’t forget to submit your Homework Excel
Program files for grading by the TA
![Page 4: ME 142 Engineering Computation Iemp.byui.edu/MILLERG/ME142/SM/Unit 5.3 In Class - Debugging.pdf · Testing Center Portion –11/12-11/15 Final Project Groups of 3-4 individuals Topic:](https://reader036.fdocuments.us/reader036/viewer/2022062918/5ffd185c6fde7e293b75d277/html5/thumbnails/4.jpg)
Debug Mode
Allows you step through your program and
examine the value of variables at specific
points
May step through line-by-line or set specific
breakpoints
![Page 5: ME 142 Engineering Computation Iemp.byui.edu/MILLERG/ME142/SM/Unit 5.3 In Class - Debugging.pdf · Testing Center Portion –11/12-11/15 Final Project Groups of 3-4 individuals Topic:](https://reader036.fdocuments.us/reader036/viewer/2022062918/5ffd185c6fde7e293b75d277/html5/thumbnails/5.jpg)
Debug Mode
Once in Debug Mode
may mouse-over a
variable to find its
current value:
![Page 6: ME 142 Engineering Computation Iemp.byui.edu/MILLERG/ME142/SM/Unit 5.3 In Class - Debugging.pdf · Testing Center Portion –11/12-11/15 Final Project Groups of 3-4 individuals Topic:](https://reader036.fdocuments.us/reader036/viewer/2022062918/5ffd185c6fde7e293b75d277/html5/thumbnails/6.jpg)
Debug Mode
May also hit the F8 key to single step
through the program
Each time the F8 key is hit, one line of code
is executed:
![Page 7: ME 142 Engineering Computation Iemp.byui.edu/MILLERG/ME142/SM/Unit 5.3 In Class - Debugging.pdf · Testing Center Portion –11/12-11/15 Final Project Groups of 3-4 individuals Topic:](https://reader036.fdocuments.us/reader036/viewer/2022062918/5ffd185c6fde7e293b75d277/html5/thumbnails/7.jpg)
Debug Mode
May also set a breakpoint by clicking in the
gray column on the left side of the line of
code you want to program to pause on
Click on run (from menu above) to cause the
program to resume
![Page 8: ME 142 Engineering Computation Iemp.byui.edu/MILLERG/ME142/SM/Unit 5.3 In Class - Debugging.pdf · Testing Center Portion –11/12-11/15 Final Project Groups of 3-4 individuals Topic:](https://reader036.fdocuments.us/reader036/viewer/2022062918/5ffd185c6fde7e293b75d277/html5/thumbnails/8.jpg)
Debug Mode
Another powerful feature
to use in conjunction with
the previous techniques is
the Locals Window
It displays the current
value of all local variables
in your program
![Page 9: ME 142 Engineering Computation Iemp.byui.edu/MILLERG/ME142/SM/Unit 5.3 In Class - Debugging.pdf · Testing Center Portion –11/12-11/15 Final Project Groups of 3-4 individuals Topic:](https://reader036.fdocuments.us/reader036/viewer/2022062918/5ffd185c6fde7e293b75d277/html5/thumbnails/9.jpg)
Debugging Functions
How do you debug a Function?
Set a breakpoint prior to executing the function
![Page 10: ME 142 Engineering Computation Iemp.byui.edu/MILLERG/ME142/SM/Unit 5.3 In Class - Debugging.pdf · Testing Center Portion –11/12-11/15 Final Project Groups of 3-4 individuals Topic:](https://reader036.fdocuments.us/reader036/viewer/2022062918/5ffd185c6fde7e293b75d277/html5/thumbnails/10.jpg)
PP 5.3.1
![Page 11: ME 142 Engineering Computation Iemp.byui.edu/MILLERG/ME142/SM/Unit 5.3 In Class - Debugging.pdf · Testing Center Portion –11/12-11/15 Final Project Groups of 3-4 individuals Topic:](https://reader036.fdocuments.us/reader036/viewer/2022062918/5ffd185c6fde7e293b75d277/html5/thumbnails/11.jpg)
PP 5.3.2
![Page 12: ME 142 Engineering Computation Iemp.byui.edu/MILLERG/ME142/SM/Unit 5.3 In Class - Debugging.pdf · Testing Center Portion –11/12-11/15 Final Project Groups of 3-4 individuals Topic:](https://reader036.fdocuments.us/reader036/viewer/2022062918/5ffd185c6fde7e293b75d277/html5/thumbnails/12.jpg)
PP 5.3.3
![Page 13: ME 142 Engineering Computation Iemp.byui.edu/MILLERG/ME142/SM/Unit 5.3 In Class - Debugging.pdf · Testing Center Portion –11/12-11/15 Final Project Groups of 3-4 individuals Topic:](https://reader036.fdocuments.us/reader036/viewer/2022062918/5ffd185c6fde7e293b75d277/html5/thumbnails/13.jpg)
HW 5.3.1
![Page 14: ME 142 Engineering Computation Iemp.byui.edu/MILLERG/ME142/SM/Unit 5.3 In Class - Debugging.pdf · Testing Center Portion –11/12-11/15 Final Project Groups of 3-4 individuals Topic:](https://reader036.fdocuments.us/reader036/viewer/2022062918/5ffd185c6fde7e293b75d277/html5/thumbnails/14.jpg)
HW 5.3.2
![Page 15: ME 142 Engineering Computation Iemp.byui.edu/MILLERG/ME142/SM/Unit 5.3 In Class - Debugging.pdf · Testing Center Portion –11/12-11/15 Final Project Groups of 3-4 individuals Topic:](https://reader036.fdocuments.us/reader036/viewer/2022062918/5ffd185c6fde7e293b75d277/html5/thumbnails/15.jpg)
![Page 16: ME 142 Engineering Computation Iemp.byui.edu/MILLERG/ME142/SM/Unit 5.3 In Class - Debugging.pdf · Testing Center Portion –11/12-11/15 Final Project Groups of 3-4 individuals Topic:](https://reader036.fdocuments.us/reader036/viewer/2022062918/5ffd185c6fde7e293b75d277/html5/thumbnails/16.jpg)
Key Concepts
Types of Program Errors
Debugging Tools & Techniques
Testing
VBA Help Facility
![Page 17: ME 142 Engineering Computation Iemp.byui.edu/MILLERG/ME142/SM/Unit 5.3 In Class - Debugging.pdf · Testing Center Portion –11/12-11/15 Final Project Groups of 3-4 individuals Topic:](https://reader036.fdocuments.us/reader036/viewer/2022062918/5ffd185c6fde7e293b75d277/html5/thumbnails/17.jpg)
![Page 18: ME 142 Engineering Computation Iemp.byui.edu/MILLERG/ME142/SM/Unit 5.3 In Class - Debugging.pdf · Testing Center Portion –11/12-11/15 Final Project Groups of 3-4 individuals Topic:](https://reader036.fdocuments.us/reader036/viewer/2022062918/5ffd185c6fde7e293b75d277/html5/thumbnails/18.jpg)
Types of Programming Errors
Syntax Errors
Run-Time Errors
Logic Errors
![Page 19: ME 142 Engineering Computation Iemp.byui.edu/MILLERG/ME142/SM/Unit 5.3 In Class - Debugging.pdf · Testing Center Portion –11/12-11/15 Final Project Groups of 3-4 individuals Topic:](https://reader036.fdocuments.us/reader036/viewer/2022062918/5ffd185c6fde7e293b75d277/html5/thumbnails/19.jpg)
Syntax Errors
Syntax errors occur when the rules of the
programming language are violated.
Examples:
Misspelling a keyword or function name
Omission of necessary keywords in a command.
![Page 20: ME 142 Engineering Computation Iemp.byui.edu/MILLERG/ME142/SM/Unit 5.3 In Class - Debugging.pdf · Testing Center Portion –11/12-11/15 Final Project Groups of 3-4 individuals Topic:](https://reader036.fdocuments.us/reader036/viewer/2022062918/5ffd185c6fde7e293b75d277/html5/thumbnails/20.jpg)
Run-Time Errors
Runtime errors are those errors that occur
when a program free of syntax errors, runs
and generates an error.
Examples:
Attempting an invalid operation such as division
by zero
Providing the wrong data type to a function.
![Page 21: ME 142 Engineering Computation Iemp.byui.edu/MILLERG/ME142/SM/Unit 5.3 In Class - Debugging.pdf · Testing Center Portion –11/12-11/15 Final Project Groups of 3-4 individuals Topic:](https://reader036.fdocuments.us/reader036/viewer/2022062918/5ffd185c6fde7e293b75d277/html5/thumbnails/21.jpg)
Logic Errors
Logic errors are due to incorrect program
logic.
The program may run without errors
The results it returns are inaccurate.
Often the most difficult to locate
![Page 22: ME 142 Engineering Computation Iemp.byui.edu/MILLERG/ME142/SM/Unit 5.3 In Class - Debugging.pdf · Testing Center Portion –11/12-11/15 Final Project Groups of 3-4 individuals Topic:](https://reader036.fdocuments.us/reader036/viewer/2022062918/5ffd185c6fde7e293b75d277/html5/thumbnails/22.jpg)
Debugging Tools & Techniques
![Page 23: ME 142 Engineering Computation Iemp.byui.edu/MILLERG/ME142/SM/Unit 5.3 In Class - Debugging.pdf · Testing Center Portion –11/12-11/15 Final Project Groups of 3-4 individuals Topic:](https://reader036.fdocuments.us/reader036/viewer/2022062918/5ffd185c6fde7e293b75d277/html5/thumbnails/23.jpg)
Debugging Tools & Techniques
MsgBox Function
Debug.Print Statement
Debug Mode
![Page 24: ME 142 Engineering Computation Iemp.byui.edu/MILLERG/ME142/SM/Unit 5.3 In Class - Debugging.pdf · Testing Center Portion –11/12-11/15 Final Project Groups of 3-4 individuals Topic:](https://reader036.fdocuments.us/reader036/viewer/2022062918/5ffd185c6fde7e293b75d277/html5/thumbnails/24.jpg)
MsgBox Statement
Place MsgBox statement in your code as
desired
Causes the value of the variables to be
immediately displayed in a dialog box and
pauses execution of program
Example:
MsgBox (“A = “ & A)
![Page 25: ME 142 Engineering Computation Iemp.byui.edu/MILLERG/ME142/SM/Unit 5.3 In Class - Debugging.pdf · Testing Center Portion –11/12-11/15 Final Project Groups of 3-4 individuals Topic:](https://reader036.fdocuments.us/reader036/viewer/2022062918/5ffd185c6fde7e293b75d277/html5/thumbnails/25.jpg)
Debug.Print Statement
Place Debug.Print statement in your code as
you would MsgBox function
Causes the value of the variables to be
printed in the Immediate window
Example:
Debug.Print A,B,C
![Page 26: ME 142 Engineering Computation Iemp.byui.edu/MILLERG/ME142/SM/Unit 5.3 In Class - Debugging.pdf · Testing Center Portion –11/12-11/15 Final Project Groups of 3-4 individuals Topic:](https://reader036.fdocuments.us/reader036/viewer/2022062918/5ffd185c6fde7e293b75d277/html5/thumbnails/26.jpg)
Debug.Print Statement
![Page 27: ME 142 Engineering Computation Iemp.byui.edu/MILLERG/ME142/SM/Unit 5.3 In Class - Debugging.pdf · Testing Center Portion –11/12-11/15 Final Project Groups of 3-4 individuals Topic:](https://reader036.fdocuments.us/reader036/viewer/2022062918/5ffd185c6fde7e293b75d277/html5/thumbnails/27.jpg)
Debug Mode
Allows you step through your program and
examine the value of variables at specific
points
May step through line-by-line or set specific
breakpoints
![Page 28: ME 142 Engineering Computation Iemp.byui.edu/MILLERG/ME142/SM/Unit 5.3 In Class - Debugging.pdf · Testing Center Portion –11/12-11/15 Final Project Groups of 3-4 individuals Topic:](https://reader036.fdocuments.us/reader036/viewer/2022062918/5ffd185c6fde7e293b75d277/html5/thumbnails/28.jpg)
Debug Mode
When a run-time error occurs may enter
Debug Mode by selecting button on dialog
box that appears:
![Page 29: ME 142 Engineering Computation Iemp.byui.edu/MILLERG/ME142/SM/Unit 5.3 In Class - Debugging.pdf · Testing Center Portion –11/12-11/15 Final Project Groups of 3-4 individuals Topic:](https://reader036.fdocuments.us/reader036/viewer/2022062918/5ffd185c6fde7e293b75d277/html5/thumbnails/29.jpg)
Debug Mode
Once in Debug Mode
may mouse-over a
variable to find its
current value:
![Page 30: ME 142 Engineering Computation Iemp.byui.edu/MILLERG/ME142/SM/Unit 5.3 In Class - Debugging.pdf · Testing Center Portion –11/12-11/15 Final Project Groups of 3-4 individuals Topic:](https://reader036.fdocuments.us/reader036/viewer/2022062918/5ffd185c6fde7e293b75d277/html5/thumbnails/30.jpg)
Debug Mode
May also hit the F8 key to single step
through the program
Each time the F8 key is hit, one line of code
is executed:
![Page 31: ME 142 Engineering Computation Iemp.byui.edu/MILLERG/ME142/SM/Unit 5.3 In Class - Debugging.pdf · Testing Center Portion –11/12-11/15 Final Project Groups of 3-4 individuals Topic:](https://reader036.fdocuments.us/reader036/viewer/2022062918/5ffd185c6fde7e293b75d277/html5/thumbnails/31.jpg)
Debug Mode
May also set a breakpoint by clicking in the
gray column on the left side of the line of
code you want to program to pause on
Click on run (from menu above) to cause the
program to resume
![Page 32: ME 142 Engineering Computation Iemp.byui.edu/MILLERG/ME142/SM/Unit 5.3 In Class - Debugging.pdf · Testing Center Portion –11/12-11/15 Final Project Groups of 3-4 individuals Topic:](https://reader036.fdocuments.us/reader036/viewer/2022062918/5ffd185c6fde7e293b75d277/html5/thumbnails/32.jpg)
Debug Mode
Another powerful feature
to use in conjunction with
the previous techniques is
the Locals Window
It displays the current
value of all local variables
in your program
![Page 33: ME 142 Engineering Computation Iemp.byui.edu/MILLERG/ME142/SM/Unit 5.3 In Class - Debugging.pdf · Testing Center Portion –11/12-11/15 Final Project Groups of 3-4 individuals Topic:](https://reader036.fdocuments.us/reader036/viewer/2022062918/5ffd185c6fde7e293b75d277/html5/thumbnails/33.jpg)
Testing
![Page 34: ME 142 Engineering Computation Iemp.byui.edu/MILLERG/ME142/SM/Unit 5.3 In Class - Debugging.pdf · Testing Center Portion –11/12-11/15 Final Project Groups of 3-4 individuals Topic:](https://reader036.fdocuments.us/reader036/viewer/2022062918/5ffd185c6fde7e293b75d277/html5/thumbnails/34.jpg)
Testing
Deadly Misconception of Beginning Programmers:
If a program runs without errors, the results must be correct
Very important that before you use your program to make engineering decisions (or turn it in to get a grade in class) that you perform a series of test to make certain that the output is correct.
![Page 35: ME 142 Engineering Computation Iemp.byui.edu/MILLERG/ME142/SM/Unit 5.3 In Class - Debugging.pdf · Testing Center Portion –11/12-11/15 Final Project Groups of 3-4 individuals Topic:](https://reader036.fdocuments.us/reader036/viewer/2022062918/5ffd185c6fde7e293b75d277/html5/thumbnails/35.jpg)
Testing Suggestions
Compare your results to that of a simple,
known problem by using a calculator or
Excel
Test a typical range of inputs
Test unusual but valid data
Test extreme values
![Page 36: ME 142 Engineering Computation Iemp.byui.edu/MILLERG/ME142/SM/Unit 5.3 In Class - Debugging.pdf · Testing Center Portion –11/12-11/15 Final Project Groups of 3-4 individuals Topic:](https://reader036.fdocuments.us/reader036/viewer/2022062918/5ffd185c6fde7e293b75d277/html5/thumbnails/36.jpg)
More Suggestions to Help Eradicate Program Bugs
Use VBA’s debugger tool
Structure your program with indentation for loops and if
statements
Keep your programs simple and small. Break up large
programs into smaller programs where appropriate.
Use the Option Explicit statement at the beginning of your
modules to force you to define every variable used in your
program
Use the macro recorder to help identify unfamiliar properties
and methods.
![Page 37: ME 142 Engineering Computation Iemp.byui.edu/MILLERG/ME142/SM/Unit 5.3 In Class - Debugging.pdf · Testing Center Portion –11/12-11/15 Final Project Groups of 3-4 individuals Topic:](https://reader036.fdocuments.us/reader036/viewer/2022062918/5ffd185c6fde7e293b75d277/html5/thumbnails/37.jpg)
VBA Help Facility
![Page 38: ME 142 Engineering Computation Iemp.byui.edu/MILLERG/ME142/SM/Unit 5.3 In Class - Debugging.pdf · Testing Center Portion –11/12-11/15 Final Project Groups of 3-4 individuals Topic:](https://reader036.fdocuments.us/reader036/viewer/2022062918/5ffd185c6fde7e293b75d277/html5/thumbnails/38.jpg)
In Class Activity
Use the debugger to debug the program
provided
Your result should be:
![Page 39: ME 142 Engineering Computation Iemp.byui.edu/MILLERG/ME142/SM/Unit 5.3 In Class - Debugging.pdf · Testing Center Portion –11/12-11/15 Final Project Groups of 3-4 individuals Topic:](https://reader036.fdocuments.us/reader036/viewer/2022062918/5ffd185c6fde7e293b75d277/html5/thumbnails/39.jpg)
Sub cylvol()
' Calculates the volume of a cylinder
' Input: diameter and height
' Output cylinder volume
' Input the diameter and height
h = Range("B2").Value
d = Range("C2").Value
' Calculate the volume
vol = Pi * (d ^ 2 / 2) * h
'Round and output the volume
vol = Round(vol, 2)
Range("D4").Value = volume
End Sub
![Page 40: ME 142 Engineering Computation Iemp.byui.edu/MILLERG/ME142/SM/Unit 5.3 In Class - Debugging.pdf · Testing Center Portion –11/12-11/15 Final Project Groups of 3-4 individuals Topic:](https://reader036.fdocuments.us/reader036/viewer/2022062918/5ffd185c6fde7e293b75d277/html5/thumbnails/40.jpg)
Review Questions
![Page 41: ME 142 Engineering Computation Iemp.byui.edu/MILLERG/ME142/SM/Unit 5.3 In Class - Debugging.pdf · Testing Center Portion –11/12-11/15 Final Project Groups of 3-4 individuals Topic:](https://reader036.fdocuments.us/reader036/viewer/2022062918/5ffd185c6fde7e293b75d277/html5/thumbnails/41.jpg)
Review QuestionProgramming Errors
Which of the following is an example of a
runtime error:
A. Misspelled keyword
B. Division by zero
C. Inaccurate results
D. All of the above
![Page 42: ME 142 Engineering Computation Iemp.byui.edu/MILLERG/ME142/SM/Unit 5.3 In Class - Debugging.pdf · Testing Center Portion –11/12-11/15 Final Project Groups of 3-4 individuals Topic:](https://reader036.fdocuments.us/reader036/viewer/2022062918/5ffd185c6fde7e293b75d277/html5/thumbnails/42.jpg)
Review QuestionDebugging Tools & Techniques
Which debugging tool/technique gives you the
greatest amount of power/flexibility in finding
program bugs:
A. MsgBox Function
B. Debug.Print Statement
C. Debug Mode
![Page 43: ME 142 Engineering Computation Iemp.byui.edu/MILLERG/ME142/SM/Unit 5.3 In Class - Debugging.pdf · Testing Center Portion –11/12-11/15 Final Project Groups of 3-4 individuals Topic:](https://reader036.fdocuments.us/reader036/viewer/2022062918/5ffd185c6fde7e293b75d277/html5/thumbnails/43.jpg)
Homework Help ‘n Hints