Chapter 7 What’s Wrong with It? (Syntax and Logic Errors)
Clearly Visual Basic: Programming with Visual Basic 2010
2nd Edition
Objectives
Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition2
After studying Chapter 7, you should be able to:Locate syntax errors using the Error List windowLocate a logic error by stepping through the codeLocate logic errors using breakpointsFix syntax and logic errors
There’s a Bug in my Soup!
Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition3
Downside to variables and named constantsTheir use requires additional lines of code
BugError in a program’s code
DebuggingProcess of locating and correcting any bugs in
a programProgram bugs
Typically caused by either syntax errors or logic errors
Finding Syntax Errors
Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition4
Syntax error Occurs when you break one of the
language’s rulesCode Editor
Detects most syntax errors as you enter instructions
Locating Logic Errors
Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition5
Logic error Can occur for a variety of reasonsDifficult type of error to locateCannot be detected by Code Editor
Locating Logic Errors (cont’d.)
Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition6
Debug the Discount Calculator applicationLetter D at end of a value indicates value’s
data type is Decimal
Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition7
Figure 7-7 btnCalc control’s Click event procedure
Locating Logic Errors (cont’d.)
Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition8
Figure 7-8 Result of using the Debug menu’s Step Into option
Locating Logic Errors (cont’d.)
Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition9
Use a breakpoint to pause execution at a specific line in the code
Debug the Hours Worked application.0 at end of a number indicates that
number’s data type is Double
Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition10
Figure 7-14 Breakpoint set in the procedure
Figure 7-15 Result of the computer reaching the breakpoint
Summary
Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition11
Program errors (bugs)Caused by either syntax errors or logic errors
Syntax errors in an application’s code Listed in Error List window when you start the
applicationYou can locate logic errors by stepping
through the codeTo set a breakpoint
Right-click desired line of code, point to Breakpoint, and click Insert Breakpoint
Summary (cont’d.)
Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition12
Letter D at the end of a valueIndicates value’s data type is Decimal
.0 at the end of a number Indicates that the number’s data type is
DoubleBefore viewing value stored in a control or
variable:First consider the value you expect to find