Presentation is loading. Please wait.

Presentation is loading. Please wait.

Selection Control Structures 2 (L09) * Nested if Statements * The if-else Chain * Exercise: if, if-else, nested if, and if-else chain Selection Control.

Similar presentations


Presentation on theme: "Selection Control Structures 2 (L09) * Nested if Statements * The if-else Chain * Exercise: if, if-else, nested if, and if-else chain Selection Control."— Presentation transcript:

1 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

2 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

3 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

4 if-else Nested within the if part if ( expression1) { if (expression2) statement 1; else statement 2; } else Statement3; Selection Control Structure Dr. Ming Zhang

5 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

6 if-else Nested within the else part if ( expression1) statement 1; else if (expression2) statement2; else statement3; Selection Control Structure Dr. Ming Zhang

7 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

8 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

9 Monthly Income of a Salesperson Monthly Sales (ms) Monthly Income ms >= $50000 $375+16%of sales $50000> ms >=$40000 $350+ 14% of sales $40000 > ms $325+ 12% of sales Selection Control Structure Dr. Ming Zhang

10 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=375.0+0.16*ms; else if(ms >=40000) income = 350+ 0.14*ms; else if(ms <40000) income =325+ 0.12 *ms; cout << “The income is:” << income << endl; return(0);} Selection Control Structure Dr. Ming Zhang

11 Home Work 1: Monthly Income Programming and Running Monthly Sales (ms) Monthly Income ms >= $50000 $375+16%of sales $50000 >ms >=$40000 $350+ 14% of sales $40000 >ms >=$30000 $325+ 12% 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

12 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


Download ppt "Selection Control Structures 2 (L09) * Nested if Statements * The if-else Chain * Exercise: if, if-else, nested if, and if-else chain Selection Control."

Similar presentations


Ads by Google