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..." |
|||
| (6 intermediate revisions by the same user not shown) | |||
| Line 58: | Line 58: | ||
*Buat form baru. | *Buat form baru. | ||
*Desain Tampilan form seperti gambar berikut ini: | *Desain Tampilan form seperti gambar berikut ini: | ||
[[File:Pemrograman visual perulangan 2638892.png|center|thumb]] | |||
*Tambahkan kontrol ke form seperti pada gambar diatas dan atur propertinya seperti tabel berikut: | *Tambahkan kontrol ke form seperti pada gambar diatas dan atur propertinya seperti tabel berikut: | ||
| Line 65: | Line 66: | ||
! 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 <code>Btn_if</code>, kemudian ketikkan kode program berikut ini: | |||
<syntaxhighlight lang="vbscript" line="1"> | |||
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 <code>Btn_if_else</code>, kemudian ketikkan kode program berikut ini: | |||
<syntaxhighlight lang="vbscript" line="1"> | |||
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 <code>Btn_case</code>, kemudian ketikkan kode program berikut ini : | |||
<syntaxhighlight lang="vbscript" line="1"> | |||
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 <code>F5</code> (di keyboard), atau melalui ikon <code>Start Debugging</code> di toolbar, atau melalui menu <code>Debug</code> > <code>Start Debuging</code>. | |||
*Simpan aplikasi Anda. | |||
===Pencabangan Bersarang (NESTED IF)=== | |||
*Buat project baru, dengan nama <code>Prak4</code>. | |||
*Desain Tampilan form seperti gambar berikut ini: | |||
[[File:Pemrograman visual perulangan 367483723.png|center|thumb]] | |||
*Tambahkan kontrol ke form seperti pada gambar diatas dan atur propertinya seperti tabel berikut: | |||
{| class="wikitable" | |||
|+ | |||
!Object | |||
!Properties | |||
!Nilai | |||
|- | |||
| rowspan="2" |Form1 | |||
|Name | |||
|Form1 | |||
|- | |||
|Text | |||
|Pencabangan IF | |||
|- | |||
|Label1 | |||
|Text | |||
|User Name | |||
|- | |||
|Label2 | |||
|Text | |||
|Password | |||
|- | |||
|TextBox1 | |||
|Name | |||
|TxtUserName | |||
|- | |||
| rowspan="2" |TextBox2 | |||
|Name | |||
|TxtPassword | |||
|- | |||
|PasswordChar | |||
|* | |||
|- | |||
| rowspan="2" |Button1 | |||
|Name | |||
|BtnLogin | |||
|- | |||
|Text | |||
|Login | |||
|- | |||
| rowspan="2" |Button2 | |||
|Name | |||
|BtnCancel | |||
|- | |||
|Text | |||
|Cancel | |||
|} | |||
*Klik ganda pada <code>BtnLogin</code>, kemudian ketikkan kode program berikut ini: | |||
<syntaxhighlight lang="vbscript" line="1"> | |||
If TxtPassword.Text = "" And TxtUserName.Text = "" Then | |||
MessageBox.Show("User name dan Password harus diisi", "Konfirmasi", MessageBoxButtons.OK, MessageBoxIcon.Question) | |||
TxtUserName.Focus() | |||
ElseIf TxtPassword.Text = "" Then | |||
MessageBox.Show("Password harus diisi", "Konfirmasi", MessageBoxButtons.OK, MessageBoxIcon.Question) | |||
TxtPassword.Focus() | |||
ElseIf TxtUserName.Text = "" Then | |||
MessageBox.Show("User name harus diisi", "Konfirmasi", MessageBoxButtons.OK, MessageBoxIcon.Question) | |||
TxtUserName.Focus() | |||
ElseIf TxtUserName.Text = "STIKOM" And TxtPassword.Text = "1234" Then | |||
MessageBox.Show("Password dan User Name Benar", "Konfirmasi", MessageBoxButtons.OK, MessageBoxIcon.Question) | |||
Else | |||
MessageBox.Show("Password dan User Name Anda Salah, ulangi lagi", "Konfirmasi", MessageBoxButtons.OK, MessageBoxIcon.Question) | |||
TxtUserName.Text = "" | |||
TxtUserName.Text = "" | |||
TxtUserName.Focus() | |||
End If | |||
End Sub | |||
</syntaxhighlight> | |||
*Klik ganda pada <code>BtnCancel</code>, kemudian ketikkan kode program berikut ini: | |||
<syntaxhighlight lang="vbscript" line="1"> | |||
end | |||
</syntaxhighlight> | |||
*Jalankan aplikasi dengan menekan tombol <code>F5</code> (di keyboard), atau melalui ikon <code>Start Debugging</code> di toolbar, atau melalui menu <code>Debug</code> > <code>Start Debuging</code>. | |||
*Simpan aplikasi Anda. | |||
[[Category:Materi]] | |||
[[Category:Matkul]] | |||
[[Category:Pemrograman Visual]] | |||