Title: For...Next Statements
1For...Next Statements
2For...Next Syntax
- Repeats a group of statements a specified number
of times.
- For counter start To end Step step
- statements
- Exit For
- statements
- Next counter
3For...Next Statement
- When the number of times a loop should be
executed is known in advance, a ForNext loop
should be used.
- For i m To n
- statement(s)
- Next i
initial value
terminating value
control variable (counter)
body
4ForNext Example
Private Sub cmdDisplay_Click()
Dim i As Integer 'Display a row of 10 stars
picOutput.Cls For i 1 To 10 picOutput.
Print "" Next i End Sub
5ForNext Example
Private Sub cmdDisplay_Click()
Dim sum As Single, num As Integer
sum 0 For num 1 To 99 Step 2 sum s
um num Next num picOutput.Print "The sum i
s" sum End Sub
6Private Sub cmdReverse_Click()
Dim m As Integer, j As Integer
Dim temp As String, strWord as String
m Len(txtWord.Text) strWord txtWord.Text
temp "" For j m To 1 Step -1 temp
temp Mid(strWord, j, 1) Next j picTranspos
e.Print temp End Sub
7Nested For...Next Example
- Private Sub cmdDisplay_Click()
- Dim Words, Chars As Integer
- Dim MyString As String
- MyString
- For Words 10 To 1 Step -1
- For Chars 0 To 9
- MyString MyString Chars
- Next Chars
- MyString MyString
- Next Words
- picOutput.Print MyString
- End Sub
8Private Sub cmdDisplay_Click()
Dim j As Integer, k As Integer
picTable.Cls For j 1 To 4 For k 1 To
4 picTable.Print j "x" k "" j k,
Next k picTable.Print Next j End Sub