1. Perbandingan
1) IF
Perintah If digunakan untuk membandingkan atau menyeleksi sebuah nilai.
Syntax:
IF kriteria1_terpenuhi THEN
Pernyataan_Kondisi1
END IF
Apabila proses perbandingan nilai memenuhi persyaratan maka pernyataan-pernyataan yang ada didalamnya akan dijalankan. Dan sebaliknya apabila proses perbandingan tidak memenuhi persyaratan maka VB sama sekali tidak akan menjalankan semua pernyataan yang ada didalam perintah IF diatas.
Contoh program:
Public Class Form2
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If e.KeyChar = Chr(13) Then
TextBox1.Focus()
End If
End Sub
Private Sub TextBox3_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox3.KeyPress
If e.KeyChar = Chr(13) Then
TextBox3.Focus()
End If
End Sub
Sub gabung()
ListBox1.Items.Add(Trim(TextBox1.Text) & " " & Trim(TextBox3.Text) & " " & Trim(TextBox2.Text))
End Sub
Private Sub TextBox2_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox2.KeyPress
If e.KeyChar = Chr(13) Then
Call gabung()
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Call gabung()
End Sub
End Class
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If e.KeyChar = Chr(13) Then
TextBox1.Focus()
End If
End Sub
Private Sub TextBox3_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox3.KeyPress
If e.KeyChar = Chr(13) Then
TextBox3.Focus()
End If
End Sub
Sub gabung()
ListBox1.Items.Add(Trim(TextBox1.Text) & " " & Trim(TextBox3.Text) & " " & Trim(TextBox2.Text))
End Sub
Private Sub TextBox2_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox2.KeyPress
If e.KeyChar = Chr(13) Then
Call gabung()
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Call gabung()
End Sub
End Class
2) SELECT ….CASE
Pernyataan SELECT CASE digunakan untuk membandingkan nilai dari sebuah variabel. Apabila isi variabel sama dengan Nilai1 maka Pernyataan1 akan dijalankan, apbila isi variabel sama dengan Nilai2 maka Pernyataan2 akan dijalankan. Namun apabila diantara kumpulan nilai tidak ada yang memenuhi kriteria maka perintah ini akan menjalankan semua pernyataan yang ada dalam keyword CASE ELSE.
Syntax:
SELECT CASE variabel
Case Nilai1
Pernyataan1
Case Nilai2
Pernyataan2
…..
Case Else
Pernyataan_Terakhir
END SELECT
Contoh program :
Module Module1
Sub Main()
Dim a As Integer
a=2
SELECT CASE a
Case 1
Console.WriteLine (“Nilai1 Terpenuhi”)
Case 2
Console.WriteLine (“Nilai2 Terpenuhi”)
Case 3
Console.WriteLine (“Nilai3 Terpenuhi”)
Case Else
Console.WriteLine (“Tidak ada yang memenuhi syarat”)
END SELECT
Console.ReadKey()
End Sub
End Module
2. Pengulangan (Loop)
Pengulangan berguna untuk mengulang sebuah perintah VB yang sama selama beberapa kali.
1. FOR .. NEXT
FOR digunakan untuk melakukan pengulangan perintah beberapa kali sampai kriteria yang diberikan terpenuhi.
Syntax:
For variabel = jumlahawal To jumlahakhir
Pernyataan
Next variabel
Kriteria pengulangan dalam perintah keyword For adalah sebuah variabel yang akan bertambah jumlahnya ketika menemui keyword Next, hingga nilai dari variabel ini sama dengan nilai jumlahakhir, selama nilai dari variabel belum sama dengan nilai jumlahakhir maka VB akan mengulang pernyataan. Variabel ini pada mulanya akan bernilai sama seperti nilai jumlahawal.
Contoh:
1. For
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim I As Integer
ListBox1.Items.Clear()
For I = 1 To 10
ListBox1.Items.Add(I)
Next
End Sub
2. 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(TextBox1.Text) To Val(TextBox2.Text)
ListBox1.Items.Add(I)
Next
End Sub
3. For Ganjil Genap
Private Sub Button5_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
Dim I As Integer
ListBox1.Items.Clear()
For I = Val(TextBox1.Text) To Val(TextBox2.Text)
If I Mod 2 = 0 Then
ListBox1.Items.Add("Bil Genap " & I)
Else
ListBox1.Items.Add("Bil Ganjil " & I)
End If
ListBox1.Items.Add(I)
Next
End Sub
Dim I As Integer
ListBox1.Items.Clear()
For I = Val(TextBox1.Text) To Val(TextBox2.Text)
If I Mod 2 = 0 Then
ListBox1.Items.Add("Bil Genap " & I)
Else
ListBox1.Items.Add("Bil Ganjil " & I)
End If
ListBox1.Items.Add(I)
Next
End Sub
4. For 3, -6, 9, -12,15
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(TextBox1.Text) To Val(TextBox2.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
End Class
2. DO … LOOP
Penggunaan Do Loop hampir sama dengan For… Next, tetapi dalam perintah Do Loop kita harus menambahkan sendiri nilai dari variabel untuk menghentikan lajunya proses pengulangan.
Contoh :
1. Do While
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim I As Integer = 1
ListBox1.Items.Clear()
Do While I <= 10
ListBox1.Items.Add(I)
I = I + 1
Loop
End Sub
2. Do While Next
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim I As Integer
ListBox1.Items.Clear()
I = Val(TextBox1.Text)
Do While I <= Val(TextBox2.Text)
ListBox1.Items.Add(I)
I = I + 1
Loop
End Sub
3. Do While 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()
I = Val(TextBox1.Text)
Do While I <= Val(TextBox2.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
End Class
Dim I As Integer
ListBox1.Items.Clear()
I = Val(TextBox1.Text)
Do While I <= Val(TextBox2.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
End Class
Tidak ada komentar:
Posting Komentar