19.12.2012 Views

Computer Programming Concepts and Visual Basic David I. Schneider

Computer Programming Concepts and Visual Basic David I. Schneider

Computer Programming Concepts and Visual Basic David I. Schneider

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

point. (Pressing F9 is referred to as toggling a breakpoint. You also can toggle<br />

a breakpoint with the Toggle Breakpoint option from the Debug menu.)<br />

10. Press F5 <strong>and</strong> click on the comm<strong>and</strong> button. Respond to the request by entering<br />

5. The program executes the first three lines <strong>and</strong> stops at the breakpoint. The<br />

breakpoint line is not executed.<br />

11. Open the Immediate window by pressing Ctrl+G. If necessary, clear the contents<br />

of the window. Type the statement<br />

Print “num =”; num<br />

into the Immediate window <strong>and</strong> then press Enter to execute the statement. The<br />

appearance of “num = 6” on the next line of the Immediate window confirms<br />

that the breakpoint line was not executed.<br />

12. Press F7 to return to the Code window.<br />

13. Move the cursor to the line num = num + 1 <strong>and</strong> then press Ctrl+F9 to specify<br />

that line as the next line to be executed. (You can also use the Set Next Statement<br />

option from the Debug menu.)<br />

14. Press F8 to execute the selected line.<br />

15. Press Ctrl+G to return to the Immediate window. Move the cursor to the line<br />

containing the Print method <strong>and</strong> press Enter to confirm that the value of num is<br />

now 7, <strong>and</strong> then return to the Code window.<br />

16. Move the cursor to the breakpoint line <strong>and</strong> press F9 to deselect the line as a<br />

breakpoint.<br />

17. Press F5 to execute the remaining lines of the program. Observe that the value<br />

displayed in the picture box is 9.<br />

General Comment: As you step through a program, the form will become hidden<br />

from view. However, the form will be represented by a button on the Windows<br />

taskbar at the bottom of the screen. The button will contain the name of the form.<br />

You can see the form at any time by clicking on its button.<br />

STEPPING THROUGH A PROGRAM CONTAINING A GENERAL<br />

PROCEDURE: SECTION 3<br />

The following walkthrough uses the single-stepping feature of the debugger to trace the flow<br />

through a program <strong>and</strong> a Sub procedure<br />

1. Create a form with a comm<strong>and</strong> button (cmdButton) <strong>and</strong> a picture box (picBox).<br />

Set the AutoRedraw property of the picture box to True. Then enter the following<br />

two Sub procedures:<br />

Private Sub cmdButton_Click()<br />

Dim p As Single, b As Single<br />

picBox.Cls p = 1000 ‘Principal<br />

Call GetBalance(p, b)<br />

picBox.Print “The balance is”; b<br />

End Sub<br />

Private Sub GetBalance(prin As Single, bal As Single)<br />

‘Calculate the balance at 5% interest rate<br />

Dim interest As Single<br />

interest = .05 * prin<br />

bal = prin + interest<br />

End Sub<br />

2. Press F8, click the comm<strong>and</strong> button, <strong>and</strong> press F8 again. The picBox.Cls statement<br />

is highlighted to indicate that it is the next statement to be executed.<br />

Appendix D 461

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!