For - Next Loop, Do - Loop, While - Wend Loop and Nested Loop In Visual Basic
Loop:
Loop is used to allow the execution of one or more lines of code repeatedly for a specified number of times or according to specific condition, Visual Basic provides the following kinds of loops structures.
- For - Next Loop Structure.
- Do - Loop structure.
- While - Loop structure.
For - Next Loop:
For - Next loop statement is used to repeat one statement or set of statements for specific number of times. For - Next loop is the oldest looping structure and is commonly used in all programming languages. Syntax of For - Next loop is
For CounerVariable = startValue To EndValue [Step n]
[Statement(s)]
[Exit For]
[Statement(s)]
Next [CounterVariable [, CounterVariable]...]
Description about the components of For - Next lop is
- CounterVariable: CounterVariable can be any numeric variable, but can't be an array or record Variable.
- SartValue: StartValue represents the initial value of counter variable that may be positive or negative or zero.
- EndValue: EndValue represnt the final value of CounterVariable, which is used to stop the execution of loop and may be positive negative or zero.
- Statements: One or more valid statements.
- Step n: The value of CounterVariable changes in the loop through step option. If step is omitted the default increment of VounterVariable is 1.
- Exit For: Exit For is used only in For - Next statement. When Exit For is executed in For - Next statement, then control is transferred to the statement following to the Next keyword.
- Next: When Next keyword is encountered, then CounterVariable is changed by value specified in Step and compared w ith the final value (i.e. EndValue ). If CounterVariable is not given with Next statemnt, then Next keyword matches the most recent For - Next statement .
It is important to note that execution of the body of this loop depends on the setting of StartValue, EndValue and Increment. If StartValue is less than EndValue then Step must used positive Values otherwise used negative values.
Example:
Start Visual Basic with new Standard EXE project, place a CommandButton on a Form and write following code in Click event of the CommandButton.
Private Sub Command1_Click ()
Dim cv As
Integer
For cv = -3 To 2
Print cv
Next cv
End Sub
|
Output
-3
-2
-1
0
1
2
|
Press F5 key to run this project and Click the CommandButton. The output will be displayed on the Form as shown above.
Do - Loop:
This loop statement is used to repeat one or more statements while or until condition is True. Syntax of Do - Loop is
Do [{while/Until} condition} Or Do
[Statement(s) Block1] [Statement(s) Block1]
[Exit Do] [Exit Do]
[Statement(s) Block2] [Statement(s) Block2]
Loop Loop [{while/Until} condition}
Description about component of So - Loop is
- Condition: Condition is any numeric expression that evaluates as True (Non Zero) or False (Zero).
- Statement(s) Block: This statement is used in Do - Loop to transfer the control to the statement following the keyword Loop. The use of While and Until option in Do - Loop statement is optional.
Example:
Start Visual Basic with new Standard EXE project, place a CommandButton on a Form and write following code in Click event of the CommandButton.
Private Sub Command1_Click ()
Dim I As
Integer
I = 1
Do
Print I
l =
I + 1
If I >
3 then
Exit
Do
End If
Loop
End Sub
|
Output
1
2
3
|
Press F5 key to run this project and Click the CommandButton. The output will be displayed on the Form as shown above. When while option is used in Do - Loop statement the body of loop will be executed till condition remains True.
While -Wend Loop:
While - Wind Loop is used to execute a single statement or a set of statements as long as specified condition remains True. The structure of While - Wend Loop is not so powerful and flexible as the structure of Do - Loop. Syntax of While - Wend loop is as under
While Condition
Statement(s)
Wend
Description about the component of While - Wend loop is as under.
- Condition: Condition is any numeric expression that evaluate as True (i.e. non Zero ) or False (i.e. Zero).
- Statemants: Statements block represents set of any valid statements.
Example:
Start Visual Basic with new Standard EXE project, place a CommandButton on a Form and write following code in Click event of the CommandButton.
Private Sub Command1_Click ()
Dim I As
Integer
While I
<= 3
Print
I
I = I
+ 1
Wend
End Sub
|
Output
1
2
3
|
Press F5 key to run this project and Click the CommandButton. The output will be displayed on the Form as shown above.
Nested Loop:
When one Loop statement (i.e. For - Next loop, While - Wend or Do - Loop ) is used within the body of another loop statement then it is called loop structure. In Visual Basic nesting process can be done at many levels.
Example:
Start Visual Basic with new Standard EXE project, place a CommandButton on a Form and write following code in Click event of the CommandButton.
Private Sub Command1_Click ()
Dim I As
Integer, J As Integer
Print “ I”,
“J”
For I = 1
To 2
For J =
1 To 2
Print I, J
Next J
Next I
For I = 1
To 2
For J =
1 To 2
Print I, J
Next
Next
For I = 1
To 2
For J =
1 To 2
Print I, J
Next J,
I
End Sub
|
Output
I J
1 1
1 2
2 1
2 2
1 1
1 2
2 1
2 2
1 1
1 2
2 1
2 2
|
Press F5 key to run this project and Click the CommandButton. The output will be displayed on the Form as shown above.
Me : If you need more help, want to ask feel free to use below comment box. It will be my pleasure to help you. THANKS for reading.