Compound Statements If you want to do more than one statement if an if- else case, you can form a block of statements, or compound statement, by enclosing the list of statements in curly braces ({ }). The statements in side of the braces will be executed sequentially, and the block counts as one statement.
Compound Statement Example if (a > b) { c = a; cout << a << endl; } else { c = b; cout << b << endl; }
IF statement If there is nothing to do if the test is fals e, then the else part of the statement can be omitted, leaving just an if statement: if (n > 0) n = n + 1;
Nested If statements If statements can be nested, that is, the statement to be done when the test is true (or false) can also be an If statement. Consider printing out three numbers in order:
An Extended Example if(a > b) if(b > c) cout << a << “ “ << b << “ “ << c << endl; else // b <= c if(a > c) cout << a << “ “ << c << “ “ << b << endl; else // a <= c cout << c << “ “ << a << “ “ << b << endl; else // a <= b if(a > c) cout << b << “ “ < a << “ “ << c << endl else // a <= c...
Multiway If-else Statement When the statement that is done when the test is false is also an If statement, it is customary to write the else and the if on one line: if(b > c) cout << a << “ “ << b << “ “ << c << endl; else if(a > c) is written if (b > c) cout << a << “ “ << b << “ “ << c << endl; else if (a > c)
More on Multi-way If Statements This is common when there are a number of possibilities based on the value of a variable: if (bedSoftness > 10) cout << “Too hard!” << endl; else if(bedSoftness > 5) cout << “Just right!” << endl; else cout << “Too soft!” <<< endl;
Dangling Else Problem Consider the following code: if (a > b) if (b > c) cout << c << endl; else cout << b << endl; Does the else go with if (a > b) or with if (b > c) ?
Answer It goes with the last if statement that is without an else, that, is if (b > c).
The Switch Statement If there are many cases the depend on the exact value of an int or char variable, rather than on ranges, we could use a multi-way if statement: if (class == 1) cout << “COSC 150” << endl; else if (class == 2) cout << “COSC 160” << endl; else if (class == 3) cout << “COSC 507” << endl;
The Switch Statement (cont'd) Or, we could use the switch statement, which implements multi-way branches based on the value of an expression. Switch (class) { case 1: cout << “COSC 150” << endl; break; case 2: cout << “COSC 160” << endl; break; case 3: cout << “COSC 507” << endl; break; default: cout << “Illegal value” << endl; break; }
The Switch statement (cont'd) What the switch statement does depends on the controlling expression, given in parentheses. If the value matches one of the cases, that code is done. Without the break statement, the next case is also done. The break statement forces the program to bypass the rest of the cases. The default case is done if the value doesn't match any of the cases.
The Break Statement The use (or non-use) of the break statement gives the programming greater flexibility. You decide if the next case is to be done or not. The reason you might want the next case to be done is that often different values should be processed the same way: switch (inputCommand) { case 'A': case 'a': cout << “command a”; break; case 'C': case 'c': cout << “command c”; break; }
Multi-way If vs. Switch The switch statement is more compact than the multi-way if statement and should be used when the code to be done is based on the value of an int or char expression. However, the switch statement cannot be used for string or floating point expressions and so the multi-way if statement must be used.
Enumerated Types Often, a problem uses a set of named literals (constants), e.g., the months of the year, or the days of the week. Good programming practice says that we should use mnemonic names rather that numbers to represent such items. Instead of creating twelve (or seven) constants, we can use an enumerated type – that is create a set of new literals to represent the items.
Enumerated Type Examples enum Months { JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEPT, OCT, NOV, DEC }; enum Days { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY }; You could also use enumerated types to assign integer values to the literals: enum MyNums { ONE=1, TWO=2, THREE=3 };
The Conditional Operator The conditional operator (my personal favorite of all the operators) is a trinary operator (three operands) which evaluates a test (Boolean expression) and based on the value of the expression, takes its value from one of two other expressions (the true-case value or the false-case value). It is similar to an if- else statement, but can be used in an expression.
Conditional Operator Example cout b ? a : b) << endl; is more succinct than the if-else statement if (a < b) cout << a << endl; else cout << b << endl;
Exercise Write a C++ program that reads in a char from the user and then a number (double). If the char is 'C' or 'c', then the number is the radius of a circle. If the char is 'S' or 's', then the number is the side of a square. If the letter is 'G' or 'g' then the number is the radius of a sphere. The program should then print out the area or volume of the object.
Exercise (cont'd) Your program should: Read in a char shape Read in a double d Use a multi-way if statement or a switch statement to distinguish the choices Calculate the area (vol) of the object and print it out Circle = PI * d^2; Square == d^2; Sphere: 4/3 * PI * d^3