Berikut adalah looping dalam Visual Basic .Net :
1. For Next Loop
For count_variable [ As datatype ] = begin To last (conditions)
[ statements ]
[ statements ]
.
.
[ statements ]
Next
Contoh :
Import System
Public Class Test
Public Share Sub MainDim a As integerSystem.Console.Writeline("Looping dengan For Next Loop ")For a = 1 to 10System.Console.Writeline(" Nilai a : ", a )NextEnd Sub
End Class
2. Do Loop
Do
[ statements ]
[ statements ]
.
.
[ statements ]
Loop
Contoh :
Import System
Public Class Test
Public Share Sub MainDim a As integerSystem.Console.Writeline("Looping dengan do loop ")DoSystem.Console.Writeline(" Nilai a : ", a )a = a + 1Loop While ( a <= 10 )End Sub
End Class
3. While Loop
While
[ statements ]
[ statements ]
.
.
[ statements ]
End While
Contoh :
Import System
Public Class Test
Public Share Sub MainDim a As integerSystem.Console.Writeline("Looping dengan while loop ")While a <= 10System.Console.Writeline(" Nilai a : ", a )a = a + 1End WhileEnd Sub
End Class
4. For Each Next Loop
For Each element as Datatype
Statement
Statement
Statement
.
.
Statement
Next
Contoh :
Import System
Public Class Test
Public Share Sub MainDim aArray() As integer = {15, 25, 55, 75, 95}Dim arrayItem as integerSystem.Console.Writeline("Looping dengan For Each Next Loop ")For Each arrayItem in aArrayConsole.Writeline(" Nilai a : ", arrayItem )NextConsole.ReadLine()End Sub
End Class
5. Nested Loop
For count1 as datatype= begin1 To finish1
For count2 as datatype = begin2 To finish2StatementStatement..StatementNext
Next
Contoh :
Import System
Public Class Test
Public Share Sub Main
Dim x, z As integer
For x = 1 to 10
for y = 2 to x' jika tidak sisa bagi 0If (( x mod y ) = 0 ) ThenExit ForEnd IfNext yif ( x > ( x \ j)) ThenConsole.Writeline(" Nilai x : ", x )End if
Next x
Console.ReadLine()
End Sub
End Class