Control Structure (BRANCHING - LOOP)

Do Loop

There are different forms of Do Loops in Visual Basic 6. These are 
i) Do While...Loop, 
ii) Do...Loop While
iii) Do...Loop Until.

i) Do While...Loop: 
The Do While...Loop structure is used to execute statements repeatedly based on a certain condition.
It first tests the condition then evaluates the loop. If the condition is True, the statements block is executed. If the condition is false, the statements inside the loop are not executed and the control is transferred to the line after the loop statements.
The repeated execution of the statements inside the loop goes on as long as the condition is true. When the condition becomes False, it exits the loop.

Syntax:
             Do While Condition
                       statement(s)
             Loop

Example: To print from 0 to 9.
Dim num As Integer
num = 0
Do While num < 10
    Print num
    num = num + 1
Loop

The program first checks the condition. If num is less than 10, the statements inside the Do While...Loop are executed. Again, the condition is checked. If it is True, the statements are executed. In this way, the iterative execution goes on. When the value of num becomes 10, the loop ends.

ii) Do...loop while: 
This loop structure first executes the statements and after that tests the condition for the repeated execution.

Syntax:
              Do
                     statement(s)

              Loop while Condition

Example: To print from 0 to 10.
Dim num As Integer
num = 0
Do
    Print num
    num = num + 1
Loop While num <= 10

Another example: Though the condition does not satisfy, the program will print 11  as this loop structure first executes the statements and after that tests the condition.
Dim num As Integer
num = 11
Do
    Print num
    num = num + 1
Loop While num < 10

Output: 11

iii) Do...loop until: 
The Do...Loop Untill structure executes the statements repeatedly until the condition is met. It is an infinite loop if the condition does not satisfy. In this case, the program cannot be stopped. Press Ctrl+Break key combination to force it to stop. The loop continues until the condition is met. The repeated execution of the statements stops when the condition is met.

Syntax:
        Do
              statement(s)

         Loop Until Condition

Example:
'x is incremented until x becomes greater than 10 

Dim x As Integer
x = 0

Do
    x = x + 1

Loop Until x > 10
    

MsgBox x

For...Next Loops

Example: To print 0 to 10.
Dim i As Integer
For i = 0 To 10
    Print i
Next i

When the value of i is 0, the Print statement is executed then i is incremented to 1. It checks whether the value of i is from 0 to 10. If it satisfies, the Print statement is executed again. In this way, the loop goes on until the value of i exceeds 10. Every time, the value of i is incremented by 1.



Example: To print 0 to 6 in steps of 2.
Dim i As Integer
For i = 0 To 6 Step 2
    Print i
Next i

Every time, the value of i is incremented by 2.


Output:
0
2
4



Example: To print in descending order from 10 to 0 in step of -3.
Dim i As Integer
For i = 10 To 0 Step -3
    Print i
Next i

Every time, the value of i is decremented by 3.


Output:
10
7
4
1


Exit For and Exit Do statement  

A For Next Loop can be terminated by an Exit For statement and a Do loop can be terminated by an Exit Do statement.

Example: Exit For statement :
Dim i As Integer
For i = 0 To 10
    If i = 3 Then
    Exit For
    End If
Print i
Next i

Output:
0
1
2



Example: Exit Do statement
Dim num As Integer
num = 0
Do While num < 10
    Print num
    num = num + 1

    If num = 4 Then
        Exit Do
    End If

Loop

Output:
0
1
2
3

Program in Visual Basic to generate the following pattern of stars:

*
* *
* * *
* * * *
* * * * *


Step-1 Drag and drop a command button on the form. Set its caption to ‘Print’
Step-2 Write the following code on the click event of command button

Private Sub Command1_Click()
        Dim i As Integer
        Dim j As Integer
        For i = 1 To 5 Step 1
        
        For j = 1 To i
                        Print " * ";
                Next j
                Print
        Next i
End Sub