Download presentation
Presentation is loading. Please wait.
Published byGeorgina Maxwell Modified over 9 years ago
1
ME 142 Engineering Computation I Debugging Techniques
2
Key Concepts Types of Program Errors Debugging Tools & Techniques Testing VBA Help Facility
3
Types of Programming Errors
4
Syntax Errors Run-Time Errors Logic Errors
5
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.
6
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.
7
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
8
Debugging Tools & Techniques
9
MsgBox Function Debug.Print Statement Debug Mode
10
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)
11
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
12
Debug.Print Statement
13
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
14
Debug Mode When a run-time error occurs may enter Debug Mode by selecting button on dialog box that appears:
15
Debug Mode Once in Debug Mode may mouse-over a variable to find its current value:
16
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:
17
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
18
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
19
Testing
20
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.
21
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
22
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.
23
VBA Help Facility
24
In Class Activity Use the debugger to debug the program provided Your result should be:
25
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
26
Review Questions
27
Review Question Programming 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
28
Review Question Debugging 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
29
Homework Help ‘n Hints
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.