Selection Control Structures 2 (L09) * Nested if Statements * The if-else Chain * Exercise: if, if-else, nested if, and if-else chain Selection Control Structure Dr. Ming Zhang
Nested if Statement * The inclusion of one or more if statements within an existing if statement is called a nested if statement. * if ( expression1) { if (expression2) statement 1; } else Statement3; Selection Control Structure Dr. Ming Zhang
Example 1 of Nested if Statement if (hours < 9) { if (distance > 500) cout << “snap”<< endl; } else cout << “pop” << endl; Selection Control Structure Dr. Ming Zhang
if-else Nested within the if part if ( expression1) { if (expression2) statement 1; else statement 2; } else Statement3; Selection Control Structure Dr. Ming Zhang
Example 2 of Nested if Statement if (hours < 9) { if (distance > 500) cout << “snap”<< endl; else cout << “ drink” << endl; } else cout << “pop” << endl; Selection Control Structure Dr. Ming Zhang
if-else Nested within the else part if ( expression1) statement 1; else if (expression2) statement2; else statement3; Selection Control Structure Dr. Ming Zhang
Example 3 of Nested if Statement if (hours > 9) cout << “pop” << endl; else { if (distance > 500) cout << “snap”<< endl; else cout << “ drink” << endl; } Selection Control Structure Dr. Ming Zhang
Exercise: if-else Chain (C ++) #include using std::cout; using std::endl; using std::cin; int main( ) {char marcode; cout << “Enter a marital code:”; cin >> marcode; if (marcode == ‘M’) cout << “Individual is married.”<< endl; else if (marcode == ‘S’) cout << “Individual is single.” << endl; else if (marcode == ‘D’) cout << “Individual is divorced.” << endl; return (0);} Selection Control Structure Dr. Ming Zhang
Monthly Income of a Salesperson Monthly Sales (ms) Monthly Income ms >= $50000 $375+16%of sales $50000> ms >=$40000 $ % of sales $40000 > ms $ % of sales Selection Control Structure Dr. Ming Zhang
Exercise:Monthly Income (C++) #include using std::cout; using std::cin; using std::endl int main( ) { int ms; float income; cout << “Enter the value of monthly sales:”; cin >> ms; if(ms >= 50000) income= *ms; else if(ms >=40000) income = *ms; else if(ms <40000) income = *ms; cout << “The income is:” << income << endl; return(0);} Selection Control Structure Dr. Ming Zhang
Home Work 1: Monthly Income Programming and Running Monthly Sales (ms) Monthly Income ms >= $50000 $375+16%of sales $50000 >ms >=$40000 $ % of sales $40000 >ms >=$30000 $ % of sales $30000 >ms >=$20000 $300+9% of sales $20000 >ms >=$10000 $250+5% of sales $10000 >ms $200+3% of sales Selection Control Structure Dr. Ming Zhang
Homework 2: C++ for Gross Payment Develop a C++ program that will determine the gross pay for each of several employees. The company pays "straight-time" for the first 40 hours worked by each employee and pays "time-and-a- half" for all hours in excess of 40 hours. You are given a list of the employee of the company, the number of hours each employee worked last week, and the hourly rate of each employee. Your program should input this information for only one employee, and should determine and display the employee's gross pay. Selection Control Structure Dr. Ming Zhang