Ads 468x60px

Monday, 23 November 2015

Struktur Pengulangan di VB.NET

Ada 4macam struktur pengulangan :
1.For-Next
2.Do While - Loop
3.Do Until - Loop
4.While - End While

untuk mencobanya mari kita buka VB.NET    


Pilih New Project->Windows Form Application->beri nama Struktur Pengulangan VB.NET->OK
tambahkan 5 button dan 1 listbox.
Edit text button seperti ini :
 
Masukan coding berikut :
disertai penjelasan sederhana

Public Class Form1
    Dim i As Integer
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        ListBox1.Items.Clear()
        For Me.i = 1 To 10 'Pengulangan dilakukan dari 1 sampai 10
            ListBox1.Items.Add(i)
        Next
    End Sub

    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        ListBox1.Items.Clear()
        i = 1
        Do While i <= 10 'kondisi i <= 10 true jadi program akan berkerja selama kondisi masih true
            ListBox1.Items.Add(i)
            i = i + 1
        Loop
    End Sub

    Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
        ListBox1.Items.Clear()
        i = 1
        Do Until i > 10 'kondisi i > 10 adalah false karena i = 1 karena itu program akan berkerja hingga kondisi true
            ListBox1.Items.Add(i)
            i = i + 1
        Loop
    End Sub

    Private Sub Button4_Click(sender As System.Object, e As System.EventArgs) Handles Button4.Click
        ListBox1.Items.Clear()
        i = 1
        While i <= 10 'sama dengan do While
            ListBox1.Items.Add(i)
            i = i + 1
        End While
    End Sub

    Private Sub Button5_Click(sender As System.Object, e As System.EventArgs) Handles Button5.Click
        ListBox1.Items.Clear()
    End Sub

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

    End Sub
End Class


Hasil :

 Selamat Mencoba ^_^