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.

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

picValues.Print index;<br />

Next index<br />

End Sub<br />

[Run, type 3.2 <strong>and</strong> .5 into the text boxes, <strong>and</strong> click the comm<strong>and</strong> button.]<br />

In the examples considered so far, the control variable was successively increased until<br />

it reached the terminating value. However, if a negative step value is used <strong>and</strong> the initial value<br />

is greater than the terminating value, then the control value is decreased until reaching the<br />

terminating value. In other words, the loop counts backward or downward.<br />

EXAMPLE 3<br />

The following program accepts a word as input <strong>and</strong> displays it backward.<br />

Private Sub cmdReverse_Click()<br />

picTranspose.Cls<br />

picTranspose.Print Reverse(txtWord.Text)<br />

End Sub<br />

Object Property Setting<br />

Private Function Reverse(info As String) As String<br />

Dim m As Integer, j As Integer, temp As String<br />

m = Len(info)<br />

temp = “”<br />

For j = m To 1 Step -1<br />

temp = temp + Mid(info, j, 1)<br />

Next j<br />

Reverse = temp<br />

End Function<br />

[Run, type SUEZ into the text box, <strong>and</strong> click the comm<strong>and</strong> button.]<br />

frm6_3_3 Caption Write Backwards<br />

lblWord Caption Enter Word<br />

txtWord Text (blank)<br />

cmdReverse Caption Reverse Letters<br />

picTranspose<br />

Note: The initial <strong>and</strong> terminating values of a For...Next loop can be expressions. For instance,<br />

the third <strong>and</strong> fifth lines of the function in Example 3 can be consolidated to<br />

For j = Len(info) To 1 Step -1

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

Saved successfully!

Ooh no, something went wrong!