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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

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

will be highlighted <strong>and</strong> executed, <strong>and</strong> then the statement containing Input-<br />

Box will be highlighted.<br />

3. Press F8 once to execute the statement containing InputBox. Type a wage of<br />

3.25 <strong>and</strong> press the Enter key. The If statement is highlighted, but has not been<br />

executed.<br />

4. Press F8 once <strong>and</strong> notice that the highlight for the current statement has jumped<br />

to the statement picBox.Print “Below minimum wage.” Because the condition<br />

“wage < 5.15” is true, the action associated with Then was selected.<br />

5. Press F8 to execute the picBox.Print statement. Notice that Else is skipped <strong>and</strong><br />

End If is highlighted.<br />

6. Press F8 again. We are through with the If block <strong>and</strong> the statement following<br />

the If block, End Sub, is highlighted.<br />

7. Click on the End icon to end the program.<br />

8. If desired, try stepping through the program again with 5.75 entered as the<br />

wage. Since the condition “wage < 5.15” will be false, the Else action will be<br />

executed instead of the Then action.<br />

SELECT CASE BLOCKS<br />

The following walkthrough illustrates how a Select Case block uses the selector to choose<br />

from among several actions.<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 open the Code<br />

window <strong>and</strong> enter the following procedure:<br />

Private Sub cmdButton_Click()<br />

Dim age As Single, price As Single<br />

picBox.Cls age = Val(InputBox(“age:”))<br />

Select Case age<br />

Case Is < 12<br />

price = 0<br />

Case Is < 18<br />

price = 3.5<br />

Case Is >= 65<br />

price = 4<br />

Case Else<br />

price = 5.5<br />

End Select<br />

picBox.Print “Your ticket price is “;FormatCurrency(price)<br />

End Sub<br />

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

statement will be highlighted <strong>and</strong> executed, <strong>and</strong> then the statement containing<br />

InputBox will be highlighted.<br />

3. Press F8 once to execute the statement containing InputBox. Type an age of 8<br />

<strong>and</strong> press the Enter key. The Select Case statement is highlighted, but has not<br />

been executed.<br />

4. Press F8 twice <strong>and</strong> observe that the action associated with Case Is < 12 is highlighted.<br />

5. Press F8 once to execute the assignment statement. Notice that End Select is<br />

highlighted. This demonstrates that when more than one Case clause is true,<br />

only the first is acted upon.<br />

Appendix D 463

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

Saved successfully!

Ooh no, something went wrong!