Struktur keputusan IF berguna untuk menyeleksi suatu syarat/kondisi.
1. IF….Then (if tunggal)
Struktur If…Then berguna untuk menilai satu kondisi yang bernilai benar saja.
Syntax: IF kondisi Then statement
2. IF…Then….Else (If ganda)
Struktur If…Then …Else berguna untuk menilai satu atau dua kondisi.
Syntax: IF kondisi Then
Statement1 àbagian true (benar)
Statement2
Else
Statement3 àbagian false (salah)
End If
3. IF…Then….Elseif….Then….Else.. (If Majemuk)
Struktur IF…Then….Elseif….Then….Else.. berguna untuk menilai tiga atau lebih Kondisi.
Syntax: IF kondisi Then
Statement1
Elseif kondisi Then
Statement2
Elseif kondisi Then
Statement3
Else
Statement4
End If
Public Class Form2
Private Sub btnproses_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnproses.Click
If nilai.Text >= 50 Then
keterangan.Text = "LULUS"
Else
keterangan.Text = "GAGAL"
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MsgBox("yakin Ingin Keluar???", MsgBoxStyle.YesNoCancel)
End
End Sub
End Class
Contoh Program kedua:
Listing programnya:
Public Class Form1
Private Sub bproses_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bproses.Click
If nilai.Text >= 80 Then
Nhuruf.Text = "A"
ElseIf nilai.Text >= 70 Then
Nhuruf.Text = "B"
ElseIf nilai.Text >= 60 Then
Nhuruf.Text = "C"
ElseIf nilai.Text >= 50 Then
Nhuruf.Text = "D"
Else
Nhuruf.Text = "E"
End If
End Sub
Private Sub nilai_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles nilai.KeyPress
Dim tombol As Integer = Asc(e.KeyChar)
If tombol = 13 Then
bproses_Click(sender, e)
End If
End Sub
Private Sub nilai_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles nilai.TextChanged
End Sub
End Class
Semoga bermanfaat..!!!
Tidak ada komentar:
Posting Komentar