Pert.8 VB (looping)

Struktur kontrol Perulangan.

Dalam Visual basic kita kenal 2 struktur control yaitu :
1. For…Next
bu:
For (pencacah) = (awal) to (akhir)
(statemen)
Next(pencacah)

2. Do…Loop
bu :
a. Do While (kondisi)
(blok)
Loop
b. Do Until (kondisi)
(blok)
Loop
Contoh Program Sederhana

1. Buatlah Tampilan seperti ini :
coba5.jpg

Listing Program :

Dim i As Integer
Dim angka as string

Private Sub Command1_Click()
For i = 0 To 5
List1.AddItem i
Next i
End Sub

Private Sub Command2_Click()
i = 1
Do While i <= 5
List2.AddItem i
i = i + 1
Loop
End Sub

Private Sub Command3_Click()
i = 0
Do Until i > 5
i = i + 1
List3.AddItem i
Loop
End Sub

Private Sub Command4_Click()
For i = 1 To 3
angka = “”
For j = 1 To i
angka = angka & j & ” ”
Next j
List4.AddItem angka
Next i
End Sub

2. Buatlah Tampilan seperti ini :
coba2.jpg

Listing program :

Dim i, isi As Integer
Dim nama(20) As String

Private Sub bersih()
Text1.Text = “”
List1.Clear
Text1.SetFocus
End Sub

Private Sub Command1_Click()
Label1.Visible = True
Text1.Visible = True
bersih
End Sub

Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
isi = 1
i = Val(Text1.Text)
Label1.Visible = False
Text1.Visible = False
Do While (isi <= i)
nama(isi) = InputBox(“Masukan Nama ke- ” + Str(isi), “Menginput Nama sebanyak ” + Str(i) + “Kali”)
List1.AddItem nama(isi)
isi = isi + 1
Loop
End If
End Sub

Tampilan Untuk input Box :
coba3.jpg

coba4.jpg

Selamat Mencoba….

Iklan

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s