Pemprograman VB Dot Net Present by: Johanes Fernandes Andry Session 3 Struktur Keputusan dan Looping SEKOLAH TINGGI MANAJEMEN INFORMATIKA & KOMPUTER Widuri
If …. then …. else If kondisi Then blok pernyataan Else End If
Contoh 1
Coding 1 Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click If RadioButton1.Checked = True Then 'LOGOFF Shell("shutdown -l -f -t 0") Else If RadioButton2.Checked = True Then 'SHUTDOWN Shell("shutdown -s -f -t 0") 'RESTART Shell("shutdown -r -f -t 0") End If End Sub Private Sub Form1_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated RadioButton1.Checked = False RadioButton2.Checked = False Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load End Class
Keterangan RadioButton1.Checked = False RadioButton2.Checked = False Menonaktifkan tanda Check pada komponen radiobutton1-3, saat aplilkasi akan dijalankan If RadioButton1.Checked = True Then 'LOGOFF Shell("shutdown -l -f -t 0") Merupakan perintah percabangan pertama yg apabila radiobutton1 dipilih maka eksekusi untuk LogOff pada Windows dijalankan Else RESTART Shell("shutdown -r -f -t 0") Merupakan perintah percabangan kedua yg apabila radiobutton2 dipilih maka eksekusi untuk ShutDown pada windows dijalankan 'RESTART End If Merupakan perintah untuk menutup dan sekaligus untuk mengakhiri perintah percabangan pertama dan kedua
Select Case Select case mempermudah penulisan kode hanya menguji ekpresi tunggan yang dievaluasi bagian atas struktur Select Case <ekspresi> Case Nilai1 Blok pernyataan 1 Case Nilai 2 Blok pernyataan 2 End Select
Contoh 2 Select Case
Coding 2 Select Case Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ListBox1.Items.Add("Merah") ListBox1.Items.Add("Kuning") ListBox1.Items.Add("Hijau") ListBox1.Items.Add("Biru") ListBox1.Items.Add("Hitam") End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Select Case ListBox1.Text Case "Merah" Me.BackColor = Color.Red Case "Kuning" Me.BackColor = Color.Yellow Case "Hijau" Me.BackColor = Color.Green Case "Biru" Me.BackColor = Color.Blue Case Else Me.BackColor = Color.Black End Select End Class
Struktur Pengulangan For ……. Next For counter = awal To akhir [step penambahan/pengurangan] pernyataan Next Do…..Loop Do While Kondisi Blok Peryataan Loop
Contoh 3 Perulangan
While…
Coding While Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim angka As Integer ListBox1.Items.Clear() angka = 1 While angka <= Val(TextBox1.Text) ListBox1.Items.Add(angka) angka = angka + 1 End While MsgBox("Anda mencetak angka 1 sampai dengan angka " & TextBox1.Text & "", MsgBoxStyle.Information, "Pesan") End Sub
Do…While…Loop Merupakan Suatu kontrol perulangan terlebih dahulu dan bentuk pengujian dilakukan belakangan Contoh penulisan Do…While…Loop Do While {Pernyataan) {Perintah} Loop
Contoh 4 Praktek Loop
Coding 4 Loop Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Do While Val(TextBox1.Text) Mod 2 = 0 MsgBox("Angka " & TextBox1.Text & " adalah bilangan genap !") Exit Sub Loop MsgBox("Angka " & TextBox1.Text & " adalah bilangan Ganjil !") End Sub
Exception Handling Try…Catch…Finally…End Try Contoh nya: Untuk menangani suatu kesalahan dalam menjalankan aplikasi, dengan adanya perintah ini setiap kesalahan akan diatasi secara otomatis sesuai dengan perintah penaganan kesalahan yang telah anda buat Try…Catch…Finally…End Try Contoh nya: Try {Kode program yg dijalankan…} Catch ex As Exception {Kode Program Penanganan kesalahan} Finally {Kode program setelah perintah Try dan Catch} End Try
Latihan Exception Handling
Coding Exception Handling Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Try Dim Angka As Integer ListBox1.Items.Clear() Angka = 1 While Angka <= Val(TextBox1.Text) ListBox1.Items.Add(Angka) Angka = Angka + 1 End While Angka = TextBox1.Text Catch Kesalahan As Exception MsgBox("Masukkan angka!") Finally TextBox1.Focus() End Try End Sub
Array Satu Dimensi dan Multi Dimensi Suatu Array yg nilai dan ukurannya sudah ditetapkan terlebih dahulu Multi Dimensi Suatu Array yg berfungsi hampir sama dengan array satu dimensi hanya saja pada array multi dimensi ini anda dapat menyimpan beberapa data yg berlainan dengan index yg sama
Contoh Array Satu Dimensi Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim Jam(4) As String Dim i As Integer Jam(0) = "Jam" Jam(1) = "Hari ini" Jam(2) = "Adalah" Jam(3) = "Jam ?" Jam(4) = Format(TimeString) For i = 0 To 4 MsgBox("" & Jam(i), , "Praktek Ketujuh Jam Empat") Next i End Sub End Class
Contoh Array Multi dimensi Public Class Form1 Dim Nama(1, 1) As String Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Nama(0, 0) = "Muhammad Sadeli" Nama(0, 1) = "Ellies Wulandari" Nama(1, 0) = "Muhammad Dimas Putra Sadeli" Nama(1, 1) = "Elsa Putri Sadeli" ComboBox1.Items.Add("001") ComboBox1.Items.Add("002") ComboBox1.Items.Add("003") ComboBox1.Items.Add("004") End Sub Private Sub ComboBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.TextChanged If ComboBox1.Text = "001" Then TextBox1.Text = "" & Nama(0, 0) & "" Else If ComboBox1.Text = "002" Then TextBox1.Text = "" & Nama(0, 1) & "" If ComboBox1.Text = "003" Then TextBox1.Text = "" & Nama(1, 0) & "" TextBox1.Text = "" & Nama(1, 1) & "" End If End Class
Latihan
Coding Latihan Public Class Form1 Private Structure Data_karyawan Dim Kode_Karyawan As String Dim Nama_Karyawan As String Dim Jabatan As String End Structure Dim Indeks As Integer Dim Data() As Data_karyawan Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click TextBox2.Text = "" ComboBox1.Text = "" TextBox2.Focus() End Sub Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click Dim cari As String cari = InputBox("Masukkan kode karyawan !") If cari = "" Then Exit Sub Indeks = cari TextBox2.Text = Data(Indeks).Nama_Karyawan ComboBox1.Text = Data(Indeks).Jabatan TextBox1.Text = Indeks Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Data(Indeks).Nama_Karyawan = TextBox2.Text Data(Indeks).Jabatan = ComboBox1.Text If Indeks = UBound(Data) Then ReDim Preserve Data(Indeks + 1) End If Indeks = Indeks + 1 Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Indeks = 1 ReDim Data(Indeks) ComboBox1.Items.Add("Direktur") ComboBox1.Items.Add("Manager") ComboBox1.Items.Add("Supervisor") ComboBox1.Items.Add("Office Boy") End Class
PR untuk Latihan 2