Pemrograman Visual:Percabangan: Difference between revisions
Created page with "Pencabangan adalah merupakan perintah yang dapat memberikan pilihan suatu kondisi, program akan menjalankan perintah apabila suatu kondisi memenuhi syarat tertentu. Pencabangan dapat dibedakan menjadi: ===IF … THEN=== <code>If … then</code> merupakan pencabangan yang mempunyai satu pencabangan atau satu blok perintah. Format penulisannya: <syntaxhighlight lang="java"> If kondisi then [perintah] End if </syntaxhighlight> ===IF … THEN … ELSE=== Suatu perintah pe..." |
|||
| Line 65: | Line 65: | ||
! Object !! Properties !! Nilai | ! Object !! Properties !! Nilai | ||
|- | |- | ||
| Form1 || Name | | rowspan="2" |Form1 || Name|| Form1 | ||
|- | |- | ||
|Text||Pencabangan IF | |||
|- | |- | ||
|Button1 | | rowspan="2" |Button1||Name||Btn_if | ||
|- | |- | ||
|Text||IF.. ENDIF | |||
|- | |- | ||
|Button2 | | rowspan="2" |Button2||Name||Btn_if_else | ||
|- | |- | ||
|Text||IF.. ELSE.. ENDIF | |||
|- | |- | ||
|Button3 | | rowspan="2" |Button3||Name||Btn_case | ||
|- | |- | ||
|Text||CASE.. ENDCASE | |||
|} | |} | ||
* Klik ganda pada Btn_if, kemudian ketikkan kode program berikut ini: | |||
<syntaxhighlight lang="bash"> | |||
Private Sub Btn_if_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_if.Click | |||
Dim usia As String, nilai As Single, thn As Integer | |||
usia = InputBox("Usia Pegawai saat ini?", "Batasan usia pensiun", 0) | |||
nilai = Val(usia) | |||
If nilai >= 55 Then | |||
thn = nilai - 55 | |||
MessageBox.Show("usia pegawai : " & usia & " Tahun", "Konfirmasi", MessageBoxButtons.OK, MessageBoxIcon.Information) | |||
MessageBox.Show("Keterangan : Pegawai harus pensiun " & thn & " tahun yang lalu", "Konfirmasi", MessageBoxButtons.OK, MessageBoxIcon.Information) | |||
End If | |||
End Sub | |||
</syntaxhighlight> | |||
*Klik ganda pada Btn_if_else, kemudian ketikkan kode program berikut ini: | |||
<syntaxhighlight lang="bash"> | |||
Private Sub Btn_if_else_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_if_else.Click | |||
Dim usia As String, nilai As Single, thn As Integer | |||
usia = InputBox("Usia Pegawai saat ini?", "Batasan usia pensiun", 0) | |||
nilai = Val(usia) | |||
If nilai >= 55 Then | |||
thn = nilai - 55 | |||
MessageBox.Show("usia pegawai : " & usia & " Tahun", "Konfirmasi", MessageBoxButtons.OK, MessageBoxIcon.Information) | |||
MessageBox.Show("Keterangan : Pegawai harus pensiun " & thn & " tahun yang lalu", "Konfirmasi", MessageBoxButtons.OK, MessageBoxIcon.Information) | |||
Else | |||
thn = 55 - nilai | |||
MessageBox.Show("usia pegawai : " & usia & " Tahun", "Konfirmasi", MessageBoxButtons.OK, MessageBoxIcon.Information) | |||
MessageBox.Show("Keterangan : Pegawai akan pensiun " & thn & " lagi", "Konfirmasi", MessageBoxButtons.OK, MessageBoxIcon.Information) | |||
End If | |||
End Sub | |||
</syntaxhighlight> | |||
*Klik ganda pada Btn_case, kemudian ketikkan kode program berikut ini : | |||
<syntaxhighlight lang="bash"> | |||
Private Sub Btn_case_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_case.Click | |||
Dim isian As String, usia As Integer | |||
isian = InputBox("Usia anda saat ini?", "Status Anda") | |||
usia = Val(isian) | |||
MessageBox.Show("Usia Anda saat ini : " & usia & " tahun", "Konfirmasi", MessageBoxButtons.OK, MessageBoxIcon.Information) | |||
Select Case usia | |||
Case 1 To 4 | |||
MessageBox.Show("Anda sekarang tergolong balita", "Konfirmasi", MessageBoxButtons.OK, MessageBoxIcon.Information) | |||
Case 5 To 16 | |||
MessageBox.Show("Anda sekarang tergolong anak-anak", "Konfirmasi", MessageBoxButtons.OK, MessageBoxIcon.Information) | |||
Case 17 To 24 | |||
MessageBox.Show("Anda sekarang tergolong remaja", "Konfirmasi", MessageBoxButtons.OK, MessageBoxIcon.Information) | |||
Case 25 To 55 | |||
MessageBox.Show("Anda sekarang tergolong dewasa", "Konfirmasi", MessageBoxButtons.OK, MessageBoxIcon.Information) | |||
Case Is >= 56 | |||
MessageBox.Show("Anda sekarang tergolong manula", "Konfirmasi", MessageBoxButtons.OK, MessageBoxIcon.Information) | |||
Case Else | |||
MessageBox.Show("Anda salah memasukkan usia", "Konfirmasi", MessageBoxButtons.OK, MessageBoxIcon.Information) | |||
End Select | |||
End Sub | |||
</syntaxhighlight> | |||
*Jalankan aplikasi dengan menekan tombol F5 (di keyboard), atau melalui ikon Start Debugging di toolbar, atau melalui menu Debug > Start Debuging. | |||
*Simpan aplikasi Anda. | |||
===Pencabangan Bersarang (NESTED IF)=== | |||
*Buat project baru, dengan nama Prak4. | |||
*Desain Tampilan form seperti gambar berikut ini: | |||
*Tambahkan kontrol ke form seperti pada gambar diatas dan atur propertinya seperti tabel berikut: | |||