Chapter 7 What’s Wrong with It? (Syntax and Logic Errors) Clearly Visual Basic: Programming with...

12
Chapter 7 What’s Wrong with It? (Syntax and Logic Errors) Clearly Visual Basic: Programming with Visual Basic 2010 2 nd Edition

description

There’s a Bug in my Soup! Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition 3 Downside to variables and named constants Their use requires additional lines of code Bug Error in a program’s code Debugging Process of locating and correcting any bugs in a program Program bugs Typically caused by either syntax errors or logic errors

Transcript of Chapter 7 What’s Wrong with It? (Syntax and Logic Errors) Clearly Visual Basic: Programming with...

Page 1: Chapter 7 What’s Wrong with It? (Syntax and Logic Errors) Clearly Visual Basic: Programming with Visual…

Chapter 7 What’s Wrong with It? (Syntax and Logic Errors)

Clearly Visual Basic: Programming with Visual Basic 2010

2nd Edition

Page 2: Chapter 7 What’s Wrong with It? (Syntax and Logic Errors) Clearly Visual Basic: Programming with Visual…

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

Page 3: Chapter 7 What’s Wrong with It? (Syntax and Logic Errors) Clearly Visual Basic: Programming with Visual…

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

Page 4: Chapter 7 What’s Wrong with It? (Syntax and Logic Errors) Clearly Visual Basic: Programming with Visual…

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

Page 5: Chapter 7 What’s Wrong with It? (Syntax and Logic Errors) Clearly Visual Basic: Programming with Visual…

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

Page 6: Chapter 7 What’s Wrong with It? (Syntax and Logic Errors) Clearly Visual Basic: Programming with Visual…

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

Page 7: Chapter 7 What’s Wrong with It? (Syntax and Logic Errors) Clearly Visual Basic: Programming with Visual…

Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition7

Figure 7-7 btnCalc control’s Click event procedure

Page 8: Chapter 7 What’s Wrong with It? (Syntax and Logic Errors) Clearly Visual Basic: Programming with Visual…

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

Page 9: Chapter 7 What’s Wrong with It? (Syntax and Logic Errors) Clearly Visual Basic: Programming with Visual…

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

Page 10: Chapter 7 What’s Wrong with It? (Syntax and Logic Errors) Clearly Visual Basic: Programming with Visual…

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

Page 11: Chapter 7 What’s Wrong with It? (Syntax and Logic Errors) Clearly Visual Basic: Programming with Visual…

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

Page 12: Chapter 7 What’s Wrong with It? (Syntax and Logic Errors) Clearly Visual Basic: Programming with Visual…

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