Stepping through Programs Containing Selection Structures: Chapter 5


[Page 633 (continued)]

If Blocks

The following walkthrough demonstrates how an If statement evaluates a condition to determine whether to take an action:

1.

Create a form with a button (btnPush), a text box (txtBox), and the following code:

Private Sub btnPush_Click(...) Handles btnPush.Click   Dim wage As Double   wage = CDbl(InputBox("Wage:"))   If wage < 5.15 Then     txtBox.Text = "Below minimum wage."   Else     txtBox.Text = "Wage Ok."   End If End Sub



[Page 634]

2.

Place the cursor on the line beginning "Private Sub," press the right mouse button, and click on "Run to Cursor." The program will execute and the form will appear.

3.

Click on the button, and then press F8. The yellow arrow points to the statement containing InputBox.

4.

Press F8 once to execute the statement containing InputBox. Type a wage of 3.25, and press the Enter key. The If statement is highlighted, but has not been executed.

5.

Press F8 once, and notice that the yellow arrow has jumped to the statement txtBox.Text= "Below minimum wage." Because the condition "wage < 5.15" is true, the action associated with Then was chosen.

6.

Press F8 to execute the txtBox.Text statement. Notice that Else is skipped and the yellow arrow points to End If.

7.

Press F8 again. We are through with the If block, and the statement following the If block, End Sub, is highlighted.

8.

Press Ctrl + Alt + Break to terminate debugging.

9.

If desired, try stepping through the program again with 5.75 entered as the wage. Since the condition "wage < 5.15" will be false, the Else action will be executed instead of the Then action.

Select Case Blocks

The following walkthrough illustrates how a Select Case block uses the selector to choose from among several actions:

1.

Create a form with a button (btnPush) and a text box (txtBox). Double-click on the button and enter the following procedure:

Private Sub btnPush_Click(...) Handles btnPush.Click   Dim age, price As Double   age = CDbl(InputBox("Age:"))   Select Case age     Case Is < 12       price = 0     Case Is < 18       price = 3.5     Case Is >= 65       price = 4     Case Else       price = 5.5   End Select   txtBox.Text = "Your ticket price is " & FormatCurrency(price) End Sub


2.

Place the cursor on the line beginning "age =", press the right mouse button, and click on "Run to Cursor." The program will execute, and the form will appear.

3.

Click on the button. The arrow will point to the statement beginning "age =".


[Page 635]

4.

Press F8 once to execute the statement beginning "age ='. Type an age of 8, and press the Enter key. The arrow points to the Select Case statement, but the statement has not been executed.

5.

Press F8 twice, and observe that the arrow points to the action associated with "CaseIs < 12".

6.

Press F8 once to execute the assignment statement. Notice that the arrow now points to End Select. This demonstrates that when more than one Case clause is true, only the first is acted upon.

7.

Press Ctrl + Alt + Break to terminate debugging.

8.

If desired, step through the program again, entering a different age and predicting which Case clause will be acted upon. (Some possible ages to try are 12, 14, 18, 33, and 67.)




An Introduction to Programming Using Visual Basic 2005
Introduction to Programming Using Visual Basic 2005, An (6th Edition)
ISBN: 0130306541
EAN: 2147483647
Year: 2006
Pages: 164

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net