The Do Loop Statements Have Four Different Forms, As Shown Below
The Do Loop Statements Have Four Different Forms, As Shown Below
a)
Do While condition
Block of one or more VB statements
Loop
b)
Do
Block of one or more VB statements
Loop While condition
c)
Do Until condition
Block of one or more VB statements
Loop
d)
Do
Block of one or more VB statements
Loop Until condition
The For....Next Loop
Next
Example 9.3 a
Example 9.3 c
For counter=1000 to 5 step -5
counter=counter-10
If counter<50 then
Exit For
Else
Print "Keep Counting"
End If
Next
Example 9.3 d
Private Sub Form_Activate( )
For n=1 to 10
If n>6 then
Exit For
Else
Print n
Enf If
Next
End Sub
A variable number is initialized to 1 and then the Do While Loop starts. First, the condition is
tested; if condition is True, then the statements are executed. When it gets to the Loop it goes
back to the Do and tests condition again. If condition is False on the first pass, the statements
are never executed.
number = 1
While number <=100
number = number + 1
Wend
The programs executes the statements between Do and Loop While structure in any case.
Then it determines whether the counter is less than 501. If so, the program again executes the
statements between Do and Loop While else exits the Loop.
Do Until...Loop Statement
Unlike the Do While...Loop and While...Wend repetition structures, the Do Until... Loop
structure tests a condition for falsity. Statements in the body of a Do Until...Loop are
executed repeatedly as long as the loop-continuation test evaluates to False.
An example for Do Until...Loop statement. The coding is typed inside the click event of the
command button
Numbers between 1 to 1000 will be displayed on the form as soon as you click on the
command button.
Dim x As Integer
For x = 1 To 50 step 2
Print x
Next
Print num
next
In order to count the numbers from 1 yo 50 in steps of 2, the following loop can be used
For x = 1 To 50 Step 2
Print x
Next
The above coding will display numbers vertically on the form. In order to display numbers
horizontally the following method can be used.
For x = 1 To 50
Print x & Space$ (2);
Next