Pengulangan adalah suatu instruksi dalam pemrograman agar sekelompok perintah dilaksanakan dengan berulang sampai dengan suatu kondisi terpenuhi.
Berikut contoh pengulangan dalan visual studio 2008
1. For
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim I As Integer
ListBox1.Items.Clear()
For I = 1 To 10
ListBox1.Items.Add(I)
Next
End Sub
2. Do While
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim I As Integer
ListBox1.Items.Clear()
Do While I <= 10
ListBox1.Items.Add(I)
I = I + 1
Loop
End Sub
3. For Next
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Dim I As Integer
ListBox1.Items.Clear()
For I = Val(TextBox2.Text) To Val(TextBox3.Text)
ListBox1.Items.Add(I)
Next
End Sub
4. Do While Next
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
Dim I As Integer
ListBox1.Items.Clear()
I = Val(TextBox2.Text)
Do While I <= Val(TextBox3.Text)
ListBox1.Items.Add(I)
I = I + 1
Loop
End Sub
5. For Ganjil & Genap
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
Dim I As Integer
ListBox1.Items.Clear()
For I = Val(TextBox2.Text) To Val(TextBox3.Text)
If I Mod 2 = 0 Then
ListBox1.Items.Add("Bil Genap " & I)
Else
ListBox1.Items.Add("Bil Ganjil " & I)
End If
Next
End Sub
6.Do While Ganjil & Genap
Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim I As Integer
ListBox1.Items.Clear()
Do I = Val(TextBox1.Text)to Val(TextBox2.Text)
Do I Mod 2 = 0 while
ListBox1.Items.Add("Bil genap " & I)
while
ListBox1.Items.Add("Bil ganjil " & I)
Loop
End Sub
7. For 3,-6,9,-12, 15
Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
Dim I As Integer
ListBox1.Items.Clear()
For I = Val(TextBox2.Text) To Val(TextBox3.Text)
If I Mod 3 = 0 And I Mod 2 = 0 Then
ListBox1.Items.Add(I * -1)
Else
If I Mod 3 = 0 Then
ListBox1.Items.Add(I)
End If
End If
Next
End Sub
8. Do While 3,-6,9,-12, 15
Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click
Dim I As Integer
ListBox1.Items.Clear()
I = Val(TextBox2.Text)
Do While I <= Val(TextBox3.Text)
If I Mod 3 = 0 And I Mod 2 = 0 Then
ListBox1.Items.Add(I * -1)
Else
If I Mod 3 = 0 Then
ListBox1.Items.Add(I)
End If
End If
I = I + 1
Loop
End Sub
Tidak ada komentar:
Posting Komentar