Sintak Looping Visual Basic .Net

 

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 Main
Dim a As integer
        System.Console.Writeline("Looping dengan For Next Loop ")
For a = 1 to 10
System.Console.Writeline(" Nilai a : ", a ) 
Next
End Sub
End Class


2. Do Loop

Do
[ statements ]
[ statements ]
.
.
[ statements ]
Loop

Contoh :
Import System
Public Class Test
Public Share Sub Main
Dim a As integer
        System.Console.Writeline("Looping dengan do loop ")
Do 
System.Console.Writeline(" Nilai a : ", a )
a = a + 1 
Loop 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 Main
Dim a As integer
        System.Console.Writeline("Looping dengan while loop ")
While a <= 10 
System.Console.Writeline(" Nilai a : ", a )
a = a + 1 
End While
End 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 Main
Dim aArray() As integer = {15, 25, 55, 75, 95}
Dim arrayItem as integer
        System.Console.Writeline("Looping dengan For Each Next Loop ")
For Each arrayItem in aArray 
Console.Writeline(" Nilai a : ", arrayItem )
Next
        Console.ReadLine()
End Sub
End Class

5. Nested Loop

For count1 as datatype= begin1 To finish1
For count2 as datatype = begin2 To finish2
Statement
Statement
.
.
Statement
Next
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 0
If (( x mod y ) = 0 ) Then
   Exit For
End If
Next y
if ( x > ( x \ j)) Then
Console.Writeline(" Nilai x : ", x )
End if
Next x
        Console.ReadLine()
End Sub
End Class