/* This program demonstrates * 1. The nested if statement * 2. The multiple if statement * 3. The switch statement * * The Nested if statement * Since the contents of the if block (Code that is executed if the condition is true) * can consist of multiple statements enclosed in {}, we can write an if statement * inside the if block (or the else block). This process is called Nesting. The * following example shows one level of Nesting, but there is no theoretical limit to * the number of nested levels. * Also, we can have multiple if statements with nesting and nested if statements, where * the internal level is a multiple if. Any combination of control structures is allowed * as long they are syntactically OK. * * 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() { //nested if statement /* Given 3 numbers, find the largest * 1. Input - read in 3 numbers - declare 3 variables, cin (x,y,z) * 2. Find the largest * 2.1 x>y? If yes, 2.1.1. If no, 2.2 * 2.1.1 x>z? If yes, largest = x. Otherwise largest = z. Go to step 3 * 2.2 y>z? If yes, largest =y. Otherwise largest = z. * 3. Print the largest - cout */ int x,y,z; cout<<"Enter 3 distinct numbers: "; cin>>x>>y>>z; int largest; if (x > y) { if(x > z) { largest = x; } else { largest = z; } } else { if (y > z) { largest = y; } else{ largest = z; } } cout<>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<