- This program explains how to use FOR NEXT loop in VB programming.
- FOR NEXT allows us to run one or more lines of code repeatedly without any break. FOR NEXT loop will break if the condition that you are given become false, otherwise it will continue.

This program explains how to use FOR NEXT loop in VB programming.
Mainly used loops in VB.NET:
FOR NEXT Loop
FOR EACH Loop
WHILE Loop
DO WHILE Loop
FOR EACH Loop
- In FOR NEXT Loop, your desired code must be inside FOR ….NEXT Statement.
- Syntax for FOR NEXT Loop
|
1 2 3 |
For counter [ As datatype ] = start To end [ Step step ] ' Code to be executed for each value of counter. Next [ counter ] |
- Counter – Numeric value or counter for repeating the loop.
- Start To End – Value from where the loop starts and from which value it stops running loop.
Using FOR NEXT loop in VB - Complete Code
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim Counter As Integer Dim StartValue As Integer Dim EndValue As Integer StartValue = 1 EndValue = 10 For Counter = StartValue To EndValue MsgBox("Counting Starts from " & Counter & " Counting ends in " & EndValue) Next Counter End Sub End Class |
Using FOR NEXT loop in VB – Working
- Step 1: You have to declare Counter, StartValue, EndValue as integer
- Step 2: Set StartValue as 1 (here you set the value where counter starts)
- Step 3: Set EndValue as 10 (Here you set the value where the loop stops)
- Step 4: Assign the StartValue to the counter and starts loop until the counter reaches the EndValue.
- Step 5: Executing the loop body, till the counter reaches end and finally loops out.
EXIT Function
- You can use EXIT function inside the loop. It helps to exit the loop under certain condition before the counter comes to EndValue.
Using FOR NEXT loop in VB






