Review and Reinforcement: Selection Structures

Tom Kelliher, CS18

Feb. 9, 1996

if, else

1. How are the notions of true and false defined in C/C++?

2. What structure do we use in order ``to do, or not to do?''

Example: If the account balance is negative, penalize the account.

3. What structure do we use to choose between two alternatives?

Example: If the grade is greater than or equal to 60, print PASSED, otherwise print FAILED.

4. What structure do we use to choose between multiple alternatives?

Example: If the day number is one, print Monday, if the day number is two, print Tuesday, etc.

What roles can the final else play?

5. Write an if/ else to print OK if i is in the range 0 to 10, inclusive and print Whoops! otherwise.

6. What is printed by each of the following?
```1)    if (50 <= 65 <= 60)
cout << "True" << endl;
else
cout << "False" << endl;
```

```2)    i = 0;
if (i = 3)
cout << "True" << endl;
else
cout << "False" << endl;

3)    i = 0;
if (!(i = 3) == 1)
cout << "True" << endl;
else
cout << "False" << endl;
```

Boolean Operations

Truth tables for single, multiple &&'s and `||`'s, !.

switch

In what subtle, but important, ways does a switch differ from a multiway if/ else?

• Numeric vs. ``boolean'' condition
• Case label vs. complex conditions
• Fall-Through
• default vs. else

Consider converting numerics grades to letter grades. Compare and contrast the two multiway selection structures.