Didalam C#, If statement mengidentifikasi pernyataan yang menjalankan berdasarkan nilai ekspresi Boolean(true/false). Jika bernilai string, int atau yang lainnya wajib menggunakan == atau != atau yang lainnya. Sedangkan didalam VB tidak bernilai Boolean. Berikut contoh If Statement:
//C# if (condition) { then-statement; } //if dengan else if (condition) { then-statement; } else { else-statement; } // if if else else if (condition) { if (condition) { then-statement; } else { else-statement; } } else { else-statement; } // if else if else if (condition) { then-statement; } else { if (condition) { then-statement; } else { else-statement; } }
'VB If (condition) Then (statement) End If ' if else If (condition) Then (statement) Else (Else statement) End If ' if if else else If (condition) Then If (condition) Then (statement) Else (Else statement) End If Else (Else statement) End If 'if else if else If (condition) Then (statement) Else If (condition) Then (statement) Else (Else statement) End If End If 'if elseif elseif else If (condition) Then (statement) ElseIf (condition Then (statement) ElseIf (condition) Then (statement) Else (statement) End If
Dari contoh diatas dapat kita pahami setiap If didalam VB wajib ditutup dengan End If, sedangkan didalam C# tidak. Sekarang buatlah 1 project beri nama untuk C# cs_02if, untuk VB vb_02if, masukan 5 button kedalam form seperti gambar 11.1 dibawah ini:
Gambar 11.1
//C# private void button1_Click(object sender, EventArgs e) { Boolean a = true; // ini Boolean cukup = if (a = true) //karena a adalah Boolean maka cukup pakai = { MessageBox.Show("Benar 1"); } byte A = 10; //bukan Boolean pakai == if (A == 10) // lihat A disini bukan Boolean jadi pakai == { MessageBox.Show("Benar 2"); } } private void button2_Click(object sender, EventArgs e) { Boolean a = true; if (a = false) { MessageBox.Show("Benar 1"); } else { MessageBox.Show("Salah 1"); } int A = 1000; if (A != 999) // != artinya tidak sama dengan { MessageBox.Show("Benar 2"); } else { MessageBox.Show("Salah 2"); } } private void button3_Click(object sender, EventArgs e) { int A = 999; int B = 1000; if (A == 999) { if (B == 1000) { MessageBox.Show("Benar 1"); } else { MessageBox.Show("Salah 1"); } } else { MessageBox.Show("Salah 2"); } } private void button4_Click(object sender, EventArgs e) { int A = 999; int B = 1000; if (A == 999) { MessageBox.Show("Benar 1"); } else { if (B == 1000) { MessageBox.Show("Benar 2"); } else { MessageBox.Show("Salah 1"); } } } private void button5_Click(object sender, EventArgs e) { int A = 999; int B = 1000; int C = 1001; if (A == 777) { MessageBox.Show("Benar 1"); } else if (B == 2000) { MessageBox.Show("Benar 2"); } else if (C == 1111) { MessageBox.Show("Benar 3"); } else { MessageBox.Show("Tidak ada yang benar"); } }
'VB Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim A As Byte = 10 If A = 10 Then MessageBox.Show("Benar") End If End Sub Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click Dim A As Byte = 10 If A = 9 Then MessageBox.Show("Benar") Else MessageBox.Show("Salah") End If End Sub Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click Dim A As Byte = 10 Dim B As Byte = 20 If A = 10 Then If B = 10 Then MessageBox.Show("benar 1") Else MessageBox.Show("salah 1") End If Else MessageBox.Show("salah 2") End If End Sub Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click Dim A As Byte = 10 Dim B As Byte = 20 If A = 10 Then MessageBox.Show("benar 1") Else If B = 10 Then MessageBox.Show("benar 2") Else MessageBox.Show("salah 1") End If End If End Sub Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click Dim A As Byte = 10 Dim B As Byte = 20 Dim C As Byte = 30 If A = 11 Then MessageBox.Show("Benar 1") ElseIf B = 19 then MessageBox.Show("Benar 2") ElseIf C = 31 then MessageBox.Show("Benar 3") Else MessageBox.Show("Tidak ada yang benar") End If End Sub
Sampai disini cukup untuk contoh If Statement, next kita masuk ke Persamaan IIf Statement.
Password: csvb2015.blogspot.co.id
Tidak ada komentar :
Posting Komentar