T 0456 Algoritma dan Metode Object Oriented Programming
T 0456 – Algoritma dan Metode Object Oriented Programming Week 2 – Struktur Kendali dan Operator
Learning Outcome Pada akhir pertemuan ini, diharapkan: • Mahasiswa dapat menerapkan struktur kendali pemilihan dan perulangan untuk membuat program dengan C++ Buku Referensi: ØC++ - How to program, Deitel & Deitel, Prentice Hall, 2001. Websites: Øhttp: //www. deitel. com
Outline Materi • Operator • Struktur kendali Pemilihan • Struktur Kendali Pengulangan
OPERATOR
Operator adalah simbol yang mengolah nilai pada operan dan menghasilkan satu nilai baru. Setiap operator mengolah data dengan satu cara tertentu. Operator Bina Nusantara Operasi Tipe Data Contoh + Unary Plus Numeric X = +y + 3 - Unary Minus Numeric X = -y + Tambah Numeric Z=y+x - Kurang Numeric Z=y–x * Kali Numeric Z=y*x / Bagi Numeric Z=y/x % Modulus Integer Z=y%x 5
Increment dan Decrement Operator Increment Operators Decrement Operators Syntax : variable++ // post-increment ++variable // pre-increment contoh : Line. Number++; ++index; Bina Nusantara variable-- // post-increment --variable // pre-increment contoh : Line. Number--; --index; 6
Assignment Operator Bina Nusantara Assignment Operator Operasi Contoh x+=y x=x+y x + = 12 x = x + 12 x-=y x=x–y x - = 34 x = x – 34 x*=y x=x*y x * = 10 x = x * 10 x/=y x=x/y x/=2 x=x/2 x%=y x=x%y x%=3 x=x%3 7
Relational dan Logikal Operator Bina Nusantara Operator Arti Contoh && Logical AND if (I > 1 && I < 10) || Logical OR if (c == 0 || c != 9) ! Logical NOT if (!(c > 1 && c < 9)) < Less than if (i < 0) <= Less than or equal to if (i <= 0) > Greater than if (i > 10) >= Greater than or equal to if (j >= 14) == Equal to if (i == 0) != Not equal to if (i != 0) 8
STRUKTUR KENDALI PEMILIHAN
Seleksi Struktur ini digunakan untuk menguji suatu kondisi, kemudian melaksanakan satu urutan perintah. • Struktur pilihan dalam C++ meliputi : - perintah if - else - perintah switch
Perintah If C++ tidak memiliki kata kunci (key word) then pada konstruksi perintah if. • Syntax if ( condition ) statement ; atau if ( condition ) { < sequence of statement> }
Flowchart Perintah if true Kondisi false Perintah jika kondisi true
Contoh Perintah if Contoh : if ( detik == 60 ) menit = menit + 1 ; if ( sisi >0 ) { luas = panjang * lebar ; isi = luas * tinggi ; }
Contoh Program If // 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”; }
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> }
Flowchart Perintah if-else false true Kondisi Perintah False Perintah True
Perintah if-else Contoh : if ( pembagi ! = 0 ) { hasil = nilai / pembagi ; cout << “ Hasil = “ << hasil << “ n ” ; } else { hasil = 0 ; cout << “ Hasil tidak ada - pembagi = “ << pembagi << “ n ” ; }
Contoh Program if-else // 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 hurufn ”; else cout << “ Input anda bukan hurufn ”; }
Multiple if-else C++ memberikan fasilitas nested if - else untuk mendukung fleksibilitas dalam program aplikasi. • Syntax : if (tested_condition 1) statement 1 {<sequence #1 of statement>} else if (tested_condition 2) statement 2 {<sequence #2 of statement>}. . . else if (tested_condition. N) statement. N {<sequence #N of statement>}
Contoh Program Multiple if-else // 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 besarn”; else if ( c >= ‘ a ’ && c <= ‘ z ’ ) cout << “Input anda adalah huruf keciln”; else if (c >= ‘ 0’ && c <= ‘ 9’) cout << “Input anda adalah satu angkan”; else cout << “Input bukan alphanumericn”; }
Perintah Switch Syntax : switch (expression) { case constant 1_1: [ case constant 1_2: . . . ] <one or more statements> break; case constant 2_1: [ case constant 2_2: . . . ] <one or more statements> break; . . . case constant. N_1: [ case constant. N_2: . . . ] <one or more statements> break; default: <one or more statements> }
Flowchart Perintah Switch case a true false case b true false case a action(s) break case b action(s) break case z action(s) break …. case z false default action(s) true
Contoh Program Perintah Switch #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; }
STRUKTUR KENDALI PERULANGAN
Struktur Pengulangan (Loops) Merupakan fasilitas bahasa C++ yang mampu melakukan proses pengulangan, karena itu komputer dapat mengerjakan proses berulang dengan cepat, tepat dan tanpa lelah. Kontruksi pengulangan dalam C++ meliputi : – perintah for – perintah do-while – perintah while Bina Nusantara 25
Perintah for C++ memiliki dua jenis pengulangan for - fixed dan conditional. Syntax : for(<intialisasi variable >; <kondisi>; <penambahan/pengurangan variable>){ <statement>. . . <statement> } Bina Nusantara 26
Perintah for Contoh : for ( i = 0; i < 10; i++ ) cout << “ Kuadrat dari “ << i << “ = “ << i * i << “ n ”; Bina Nusantara 27
Infinite Loop C++ mengijinkan pembentukan infinite loop / open loop, dengan menghilangkan ketiga parameter yang ada pada for loop. Syntax : for( ; ; ){ <statement>. . . <statement> } Bina Nusantara 28
Perintah Do-while Loop ini dikenal dengan nama conditional loop, dimana kondisi pengulangan di periksa pada akhir blok loop. Syntax : do { <sequence of statements> } while (condition); Bina Nusantara 29
Flowchart Perintah Do-while Loop action(s) true condition false Bina Nusantara 30
Perintah Do-while Loop Contoh : int i = 2 ; do { cout << i << “ ^2 = “ << i * i << “ n ”; } while ( ++i < 11 ) ; Bina Nusantara 31
Contoh Program Do While // Contoh program C++ menggunakan do-while loop #include <math. h> void main() { char c ; double x, y ; do{ do { cout << “Masukkan Bilangan: “ ; cin >> x ; } while ( x < 0 ); y = sqrt ( x ) ; cout << “Sqrt(“ << x << “)=“<< y << “n” << “Masukkan bilangan lain? (Y/N)”; cin >> c ; cout << “ n ” ; } while ( c == ‘ Y ’ || c == ‘ y ’ ); } Bina Nusantara 32
Perintah while Loop ini juga merupakan conditional loop, dimana kondisi pengulangan diuji pada awal blok pengulangan. Syntax : while ( condition ) statement { sequence of statement } Bina Nusantara 33
Flowchart Perintah while Loop action(s) true condition false Bina Nusantara 34
Perintah While Contoh : int i = 2 ; while ( ++i < 11 ) { cout << i << “ ^2 = “ << i * i << “ n ”; }; Bina Nusantara 35
Contoh Program Perintah While // Contoh program C++ menggunakan while loop #include <math. h> void main() { char c = ‘y’ ; double x=-1, y; while ( c == ‘ Y ’ || c == ‘ y ’ ){ while ( x < 0 ) { cout << “Masukkan Bilangan: “ ; cin >> x ; } y = sqrt ( x ); cout << “Sqrt(“ << x << “)=“<< y << “n” << “Masukkan bilangan lain? (Y/N)”; cin >> c ; cout << “ n ” ; } } Bina Nusantara 36
Thank You
- Slides: 37