Page 153 - Open Soource Technologies 304.indd
P. 153
Unit 6: VB Control Structure
Function Reverse(ByVal info As String) As String
Dim m, j As Integer, temp As String =””
m = info.Length
For j = m - 1 To 0 Step -1
temp &= info.Substring(j, 1)
Next
Return temp
End Function
[Run, type “SUEZ” into the text box, and click the button.]
The initial and terminating values of a For ...Next loop can be expressions. For
instance, the third and fourth lines of the function in Example 17 can be
consolidated to
For j = info.Length - 1 To 0 Step -1
6.5.1 Declaration of Control Variables
The control variable of a For ... Next loop can be declared directly in the For statement. A
statement of the form
For i As DataType = m to n
(possibly with a Step clause) both declares the control variable i and specifies its initial and
terminating values. In this case, however, the scope of the control variable i is limited to the body
of the For ... Next loop. For instance, in Example 15, the two statements
Dim yr As Integer
For yr = 2006 to 2010
can be replaced by the single statement
For yr As Integer = 2006 to 2010
In Example 16, the two statements
Dim index as Double
For index = 0 to n Step s
can be replaced by the single statement
LOVELY PROFESSIONAL UNIVERSITY 147