download

Matakuliah
Tahun
: T0456 / Algoritma dan Metode Object
Oriented Programming
: 2007
Struktur Kendali Pemilihan
Pertemuan 5
Learning Outcomes
Pada akhir pertemuan ini, diharapkan:
Mahasiswa dapat menerapkan struktur kendali pemilihan
untuk membuat program dengan C++
Buku Referensi:
C++ - How to program, Deitel & Deitel,
Prentice Hall, 2001.
Websites:
http://www.deitel.com
3
Bina Nusantara
Outline Materi
•
•
•
•
•
•
Guna Struktur kendali Pemilihan
Perintah If
Perintah If-Else
Multiple If-Else
Perintah Switch
Contoh Program
4
Bina Nusantara
Struktur Kendali Pemilihan
Struktur ini digunakan untuk menguji suatu
kondisi, kemudian melaksanakan satu urutan
perintah.
• Struktur pilihan dalam C++ meliputi :
- perintah if
- perintah if - else
- perintah switch
5
Bina Nusantara
Perintah if
C++ tidak memiliki kata kunci (key word) then pada
konstruksi perintah if.
• Syntax
if ( condition )
statement ;
atau
if ( condition ) {
< sequence of statement>
}
6
Bina Nusantara
Flowchart Perintah if
true
Kondisi
Perintah jika kondisi true
false
7
Bina Nusantara
Perintah if
Contoh :
if ( detik = 60 )
menit = menit + 1 ;
if ( sisi >0 ) {
luas = panjang * lebar ;
isi = luas * tinggi ;
}
8
Bina Nusantara
Contoh Program
// Contoh program C++ menggunakan if
#include <iostream.h>
void main() {
double x;
cout<<“Masukkan x (jangan 0): “;
cin>>x;
if ( x != 0 )
cout<<“Reciprocal dari “<<x
<<“ adalah ”<<(1/x)<<“\n”;
}
9
Bina Nusantara
Perintah if-else
Perintah ini memberikan satu alternatif dari dua
kemungkinan, kemudian diikuti dengan urutan
perintah sebagai hasil uji boolean.
Syntax :
if (condition) {
< sequence #1 of statements>
}
else {
< sequence #2 of statements>
}
10
Bina Nusantara
Flowchart Perintah if-else
false
true
Kondisi
Perintah False
Perintah True
11
Bina Nusantara
Perintah if-else
Contoh :
if ( pembagi ! = 0 ) {
hasil = nilai / pembagi ;
cout << “ Hasil = “ << hasil << “ \n ” ;
}
else {
hasil = 0 ;
cout << “ Hasil tidak ada - pembagi = “
<< pembagi << “ \n ” ;
}
12
Bina Nusantara
Contoh Program
// Contoh program C++ menggunakan if - else
#include <iostream.h>
#include <ctype.h>
void main() {
char c ;
cout << “ Masukkan Huruf : “ ;
cin >> c ;
c = toupper(c); //ubah menjadi huruf besar
if ( c >= ‘ A ’ && c <= ‘ Z ’ )
cout << “ Input anda adalah huruf\n ”;
else
cout << “ Input anda bukan huruf\n ”;
}
13
Bina Nusantara
Multiple if - else
C++ memberikan fasilitas nested if - else untuk
mendukung fleksibilitas dalam program aplikasi.
• Syntax :
if (tested_condition1)
statement1 {<sequence #1 of statement>}
else if (tested_condition2)
statement2 {<sequence #2 of statement>}
...
else if (tested_conditionN)
statementN {<sequence #N of statement>}
14
Bina Nusantara
Contoh Program
// Contoh program C++ menggunakan multiple if-else
#include <iostream.h>
void main() {
char c ;
cout << “ Masukkan satu karakter : “ ;
cin >> c ;
if ( c >= ‘ A ’ && c <= ‘ Z ’ )
cout << “Input anda adalah huruf besar\n”;
else if ( c >= ‘ a ’ && c <= ‘ z ’ )
cout << “Input anda adalah huruf kecil\n”;
else if (c >= ‘0’ && c <= ‘9’)
cout << “Input anda adalah satu angka\n”;
else
cout << “Input bukan alphanumeric\n”;
}
15
Bina Nusantara
Perintah Switch
Perintah memiliki bentuk yang digunakan untuk pilihan
berjumlah banyak.
16
Bina Nusantara
Syntax Perintah Switch
switch (expression) {
case constant1_1:
[ case constant1_2: ...]
<one or more statements>
break;
case constant2_1:
[ case constant2_2: ...]
<one or more statements>
break;
...
case constantN_1:
[ case constantN_2: ...]
<one or more statements>
break;
default:
<one or more statements>
}
17
Bina Nusantara
Flowchart Perintah Switch
case a
true
case a action(s)
break
case b action(s)
break
case z action(s)
break
false
case b
true
false
….
case z
true
false
default action(s)
18
Bina Nusantara
Contoh Program
#include <constrea.h>
#include <ctype.h>
void main() {
int bobot, char grade;
cout << “Masukkan grade: “; cin >> grade;
grade = toupper ( grade );
switch ( grade ) {
case ‘A’ : bobot = 4; break;
case ‘B’ : bobot = 3; break;
case ‘C’ : bobot = 2; break;
case ‘D’ : bobot = 1; break;
case ‘E’ : bobot = 0; break;
default : cout << “salah input bobot“ << endl;
}
cout << “Bobot = “ << bobot << endl;
}
19
Bina Nusantara
Diskusi dan Tanya Jawab
Latihan soal
20
Bina Nusantara