Loops statement Vb.Net dan Contoh Penggunaannya
Do Loop Vb.net
Pernyataan Do Loop akan melakukan pengulangan dalam kondisi Boolean yang mana kondisi dalam keadaan True atau sampai kondisi dalam keadaan True. Pengulangan dapat dihentikan dengan pernyataan Exit do. Syntax statement Do Loop vb netDo { While | Until } conditionAtau bisa juga :
< statements >
< Continue Do >
< statements >
< Exit Do >
< statements >
Loop
Do
< statements >
< Continue Do >
< statements >
< Exit Do >
< statements >
Loop { While | Until } condition
Contoh penggunaan Do Loop dalam aplikasi console
Module Module1Hasil yang ditampilkan adalah 1,2,3,4,5...20.
Sub Main()
Dim SC As Integer = 1
Do
Console.WriteLine("Do Lop Vb Net 1-20 : {0}", SC)
SC = SC + 1
Loop While (SC <= 20)
Console.ReadLine()
End Sub
End Module
Coba lihat perbedaan menggunakan Loop While dengan menggunakan Loop Until vb net
Module Module1Akan menghasilkan 1,2,3,4,5.....19
Sub Main()
Dim SC As Integer = 1
Do
Console.WriteLine("Do Lop Vb Net 1-20 : {0}", SC)
SC = SC + 1
Loop Until (SC = 20)
Console.ReadLine()
End Sub
End Module
For Next Vb Net
Statement For Next akan melakukan pengulangan dari sejumlah pernyataan tertentu dan menghitung jumlah index sebagai interasi jumlah pengulangan. Syntax statement for next vb.netFor counter [ As datatype ] = start To end [ Step step ]
< statements >
< Continue For >
< statements >
< Exit For >
< statements >
Next < counter >
Contoh penggunaan statement for next vb net
Module Module1Akan menghasilkan 1,2,3,4,5.....100
Sub Main()
Dim SC As Byte
For SC = 1 To 100
Console.WriteLine("Loops For Next 1- 100 {0}", SC)
Next
Console.ReadLine()
End Sub
End Module
Kita akan menambahkan statemen Step pada For Next, lihat contoh berikut
Module Module1Akan menghasilkan nilai 1,6,11,16, .... 96
Sub Main()
Dim SC As Byte
For SC = 1 To 100 Step 5
Console.WriteLine("Loops For Next 1- 100 {0}", SC)
Next
Console.ReadLine()
End Sub
End Module
For Each Next Loops
Statement For Each Next akan melakukan pengulangan sekelompok dari pernyataan-pernyataan untuk semua elemen yang ada dalam index Array. Syntax penggunaan For Each Next Loops Vb.NetFor Each element [ As datatype ] In group
< statements >
< Continue For >
< statements >
< Exit For >
< statements >
Next < element >
Contoh penggunaan for Each Next dalam aplikasi console
Module Module1
Sub Main()
Dim acontohArray() As Integer = {66, 666, 66666, 666, 66}
Dim indexArray As Integer
For Each indexArray In acontohArray 'menampilkan array
Console.WriteLine(indexArray)
Next
Console.ReadLine()
End Sub
End Module
While End While Statement Vb Net
Syntax While end while
While condition
< statements >
< Continue While >
< statements >
< Exit While >
< statements >
End While
Contoh penggunaan While end while Statement
Module Module1Hasil yang dimunculkan adalah angka 50,60,70....99
Sub Main()
Dim SC As Integer = 50
While SC < 100
Console.WriteLine("While End While {0}", SC)
SC = SC + 1
End While
Console.ReadLine()
End Sub
End Module
With End With Statement VB.
Syntax With End With statement
With object
< statements >
End With
Contoh pengalikasian Statement With End With Aplikasi COnsole.
Module Module1
Public Class Blogger
Public Property Url As String
Public Property Admin As String
Public Property Umur As String
End Class
Sub Main()
Dim blog As New Blogger
With blog
.Url = "http://scqq.blogspot.com/"
.Admin = "Harison Ganteng"
.Umur = "20 tahun kelahiran 1992"
End With
With blog
Console.WriteLine(.Url)
Console.WriteLine(.Admin)
Console.WriteLine(.Umur)
End With
Console.ReadLine()
End Sub
End Module
Nested For loop Vb Net
Syntax :
For counter1 [ As datatype1 ] = start1 To end1 [ Step step1 ]
For counter2 [ As datatype2 ] = start2 To end2 [ Step step2 ]
<....>
Next [ counter2 ]
Next [ counter 1]
Contoh aplikasi console Nested For loop Vb Net
Module Module1
Sub Main()
Dim x, y As Integer
For x = 2 To 100
For y = 2 To x
' if factor found, not prime
If ((x Mod y) = 0) Then
Exit For
End If
Next y
If (y > (x \ y)) Then
Console.WriteLine("{0} is prime", x)
End If
Next x
Console.ReadLine()
End Sub
End Module
Untuk referensi : http://www.tutorialspoint.com/vb.net/vb.net_loops.htm
Oh iya buat kamu yang ingin belajar Loops pada java silhkan baca Tutorial Java : LOOPING Perulangan While, do-while dan for.
Jangan lupa dishare jika bermanfaat ya, terima kasih