1. For..Next
For-Next loop is used to iterate a set of statements based on a condition. Usually for loop is preferred when number of iterations are known in advance.
Syntax
For counter [ As datatype ] = begin To end [ Step step ]
'code
[ Continue For ]
'code
[ Exit For ]
'code
Next [ counter ]
Example
Public Module Program
Public Sub Main(args() As string)
Dim i As Integer
For i = 1 To 10 Step 1
Console.WriteLine(i)
Next
Console.ReadLine()
End Sub
End Module
Check Result here
2. For..Each
For-Each loop is used to iterate a block of code for each element in a collection.
Syntax
For Each element [ As datatype ] In group
'code
[ Continue For ]
'code
[ Exit For ]
'code
Next [ element ]
Example
Public Module Program
Public Sub Main(args() As string)
Dim num() As Integer = {1, 2, 3, 4, 5}
Dim i As Integer
For Each i In num
Console.WriteLine(i)
Next
Console.ReadLine()
End Sub
End Module
Check Result here
3. While
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations is not known in advance.
Syntax
While conditional-expression
'Code
[ Continue While ]
'Code
[ Exit While ]
'Code
End While
Example
Public Module Program
Public Sub Main(args() As string)
Dim i As Integer = 1
While i <= 10
Console.WriteLine(i)
i = i + 1
End While
Console.ReadLine()
End Sub
End Module
Check result here
4. Do-while
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
Syntax
Do { While | Until } conditional-expression
'Code
[ Continue Do ]
'Code
[ Exit Do ]
'Code
Loop
Do
'Code
[ Continue Do ]
'Code
[ Exit Do ]
'Code
Loop { While | Until } conditional-expression
Example
Public Module Program
Public Sub Main(args() As string)
Dim i As Integer = 1
Do
Console.WriteLine(i)
i = i + 1
Loop While (i <= 10)
Console.ReadLine()
End Sub
End Module