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.

112 <strong>Computer</strong> <strong>Programming</strong> <strong>Concepts</strong> <strong>and</strong> <strong>Visual</strong> <strong>Basic</strong><br />

If cond1 Then If cond1 And cond2 Then<br />

If cond2 Then action<br />

action End If<br />

End If<br />

End If<br />

FIGURE 4-2 A Confusing If Block <strong>and</strong> an Improvement<br />

4. Some programs call for selecting among many possibilities. Although such<br />

tasks can be accomplished with complicated nested If blocks, the Select Case<br />

block (discussed in the next section) is often a better alternative.<br />

5. In Appendix D, the section “Stepping Through Programs Containing Decision<br />

Structures: Section 4” uses the <strong>Visual</strong> <strong>Basic</strong> debugging tools to trace the flow<br />

through an If block.<br />

6. <strong>Visual</strong> <strong>Basic</strong> also has a single-line If statement of the form<br />

If condition Then action1 Else action2<br />

which is a holdover from earlier, unstructured versions of BASIC; it is seldom<br />

used in this text.<br />

4.3 SELECT CASE BLOCKS<br />

A Select Case block is an efficient decision-making structure that simplifies choosing among<br />

several actions. It avoids complex nested If constructs. If blocks make decisions based on the<br />

truth value of a condition; Select Case choices are determined by the value of an expression<br />

called a selector. Each of the possible actions is preceded by a clause of the form<br />

Case valueList<br />

where valueList itemizes the values of the selector for which the action should be taken.<br />

EXAMPLE 1<br />

The following program converts the finishing position in a horse race into a descriptive phrase. After the<br />

variable position is assigned a value from txtPosition, <strong>Visual</strong> <strong>Basic</strong> searches for the first Case statement<br />

whose value list contains that value <strong>and</strong> executes the succeeding statement. If the value of position is<br />

greater than 5, then the statement following Case Else is executed.<br />

Object Property Setting<br />

frmRace Caption Horse Race<br />

lblPosition Caption Finishing position (1, 2, 3, . . .)<br />

txtPosition Text (blank)<br />

cmdDescribe Caption Describe Position<br />

picOutcome<br />

Private Sub cmdDescribe_Click()<br />

Dim position As Integer ‘selector<br />

position = Val(txtPosition.Text)<br />

picOutcome.Cls Select Case position<br />

Case 1<br />

picOutcome.Print “Win”<br />

Case 2<br />

picOutcome.Print “Place”<br />

Case 3<br />

picOutcome.Print “Show”

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

Saved successfully!

Ooh no, something went wrong!