Download
selection n.
Skip this Video
Loading SlideShow in 5 Seconds..
Selection PowerPoint Presentation

Selection

124 Vues Download Presentation
Télécharger la présentation

Selection

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. Selection By: Muhammad Zidny Naf’an 27 Januari 2012

  2. Introduction • Selectionallows  a statement is executed if a condition is met or not met

  3. Selection Statements in C++ if – else switch - case

  4. “if ” one condition • Declaration if (boolean expression) statement; • or: if (boolean expression) { statement1; statement2; }

  5. “if ” one condition • Statement (statement, statement1, dan statement2) will be executed IF condition (boolean expression) TRUE,IF FALSEprogram will do nothing.

  6. Example 1 • Program to check if a number is odd or not:

  7. “If – else” with two or more conditions Declaration if (boolean expression) statement1; else statement2; or: if (boolean expression) { Statement3; Statement4; } else { Statement5; Statement6; }

  8. “If – else” with two or more condition • IF expression boolean is TRUE so statement1, statement3 and statement4 will be executed IF FALSE then statement2 , statement5 and statement6 will be executed.

  9. Example 2 • program untuk menentukan nilai terbesar dari tiga buah bilangan:

  10. Example 3 • Program to check if a number is positive, negative, or zero.

  11. switch • Change if-else declaration, • the incremental of if-else so far

  12. switch • Declaration: switch (ekspresi) { case constant_1 : statements_1; break; case constant_2 : statements_2; … case constant_N : statements_N; break; default : statements; }

  13. Example 4 #include <iostream.h> #include <conio.h> #include <stdio.h> using namespace std; int main() { int menu; cout << "*******************************" << endl; cout << "* SELAMAT DATANG DI PROGRAMKU *" << endl; cout << "* *" << endl; cout << "* silahkan pilih menu: *" << endl; cout << "*******************************" << endl << endl; cout << "1. Input Data" << endl; cout << "2. Lihat Data" << endl; cout << "3. Hapus Data" << endl; cout << "4. Edit Data" << endl; cout << "5. EXIT" << endl << endl; cout << "pilih menu: "; cin >> menu; switch(menu) { case 1: cout << "menu input data dipilih"; break; case 2: cout << "menu lihat data dipilih"; break; case 3: cout << "menu hapus data dipilih"; break; case 4: cout << "menu edit data dipilih"; break; case 5: exit(0); } getch(); }

  14. Multi Kondisidan Operator Logika Boolean expression sometime need more than one condition To gather the condition use logic operator

  15. Example 5 • Problem:student’s value index given by final test score. The rule are: • If score equal or more than 80 so index is A. • If score less than 80 and equal or more than 70 index is B. • If score less than 70 and equal or more than 60 index is C. • If score less than 60 is D.

  16. Example 5 • Solve: if(score >= 80) index= ‘A’; if(score >=70 && score<80) index = ‘B’; if(score >=60 && score<70) index = ‘C’; if(score) index = ‘D’;

  17. Example 5 /*program mencari indeks mahasiswa*/ #include <iostream.h> #include <conio.h> using namespace std; int main() { string nama_mahasiswa; float nilai; char indeks; cout << "input nama mahasiswa -> "; cin >> nama_mahasiswa; cout << "input nilai -> "; cin >> nilai; if(nilai >= 80) indeks = 'A'; if(nilai>=70 && nilai<80) indeks = 'B'; if(nilai>=60 && nilai<70) indeks = 'C'; if(nilai<60) indeks = 'D'; cout<<"indeks nilai untuk "<<nama_mahasiswa<<" adalah \'"<<indeks<< "\'"; getch(); }

  18. Exercise 1 Create a simple calculator program for add, substract, multiply, and divide. There are three inputs: operand1, operator, dan operand 2. Use switch-case or if-else

  19. Exercise 2 • Make the program to read integer from keyboard then print out month’s name that agree with the number. • Example, if we input number: 3, then program will print out “MARCH”, etc. And if we input less than 1 or more than 12 than program will print out “WRONG NUMBER”

  20. Exercise 3 • Make program to print out the capital name from given country name. • Example: if we input “indonesia” then the program will print out “JAKARTA” • The list of country and capital name for program: • You can use strcmp function.

  21. Exercise 4 • Suatu sistem penggajian di sebuah perusahaan memiliki ketentuan bahwa besarnya tunjangan yang akan diterima pegawai tergantung dari jumlah anaknya. Jika seorang pegawai memiliki anak kurang dari 3, maka tunjangannya sebesar 20% dari gaji kotor. Jika jumlah anak lebih besar dari 3 maka besarnya tunjangan adalah 30% dari gaji kotor. Selain itu juga terdapat potongan untuk keperluan asuransi. Besarnya potongan sebesar 5% dari gaji kotor jika memiliki anak sama atau kurang dari dua, dan potongan sebesar 7% jika memiliki anak lebih dari 2. Buatlah program untuk kasus tersebut. (analisis terlebih dahulu input program yang dibutuhkan, variable yang digunakan, dan algoritmanya)