/* This program demonstrates * 1. The multiple if statemet * 2. The swicth statement * 3. Fall through and character inputs for a switch statement * * Multiple if statement * A simple if else statement is only capable of implementing two * situations. If that condition were true, the if block is executed; * otherwise, the else block is executed. * If we have more than 2 situations, we can attach another if...else * statement to the else part. We can keep doing this to account for as * many situations as we want, as long as each situation can be adequately * represented by a condition. The final else (optional) can take care of * the "none of the above" case. * else and if should be separate, please don't write "elseif" * The multiple if..else statement is greedy. It will take the first branch * whose condition is "true", and ignore all others. * * The switch statement * If we have a situation where the following conditions are satisfied, we * can express the multiple if statement as a switch statement. * 1. All the conditions in the if statement should involve the same variable * 2. The variable should be an integer type (char, short, int or long) * 3. All the comparisons should be equality comparisons (==) * 4. The comparisons should involve the variable and a constant/literal * The syntax for the switch statement is given below. * switch, case, break and default are all keywords/reserved words. * We "switch" the variable involved. * Each of the constants/literals we compare the variable to would get their own "case" * The colon after the case label is not optional. * All the contents of the if block for that constant/literal are placed after the : * Once we are done with a particular case, be "break" out of the switch statement * Within the switch statement, the order of the cases don't matter. * Except for the default (none of the above) case, which should always be at the very end. * If we don't break out of the switch statement at the end of the case, control will * "fall through" and execute the statements for the following cases until it sees a * break statement or reaches the end of the switch, whichever is earlier. * To avoid this, please include a break statement after you are done with each case. */ #include using namespace std; int main() { double cp,pe,ds,exam, hw; //already weighted grades for class participation, practice // exercises, discussion sessions, exams and hw assignments cout<<"Enter the grades: "; cin>>cp>>pe>>ds>>exam>>hw; double grade = cp + ds + pe + exam +hw + 2; // 2 points for 1st day quiz, help sessions char letterGrade; // This is a multiple if statement, also called an if-else ladder if (grade >= 90) { letterGrade = 'A'; } else if (grade <90 && grade >= 80) { letterGrade = 'B'; } else if (grade <80 && grade >=70) letterGrade = 'C'; else if (grade < 70 && grade >= 60) letterGrade = 'D'; else if (grade < 60) letterGrade = 'F'; cout<<"Letter grade: "<>day; int dayofWeek = 3; if (day == 1) cout<<"Monday"; else if(day == 2) cout<<"Tuesday"; else if(day == 5) cout<<"Friday"; else if(day ==4) cout<<"Thursday"; else if(day == dayofWeek) cout<<"Wednesday"; else if(day==6) cout<<"Saturday"; else if(day ==7) cout<<"Sunday"; else cout<<"Not a valid entry"; cout<>month; int daysInMonth=0; switch(month) { case 'F': daysInMonth=28; // February break; case 'A': // April case 'j': // June case 'S': // September case 'N': daysInMonth=30; // November break; case 'J': // January case 'M': // March case 'm': // May case 'U': // July, since both J's were taken already case 'a': // August case 'O': // October case 'D': daysInMonth=31; // December break; default: cout<<"Invalid month"<