`Welcome to Lesson 9!`
Learning Objectives
By the end of today's class, you should know...
• How to make decisions when there are multiple alternatives?
• What do you do when order matters when making decisions?

Announcements

• Midterm 1 after the a short lesson and our break
• Next quiz one week from Thursday
• Yes, there is homework for tonight!
`Making Decisions Continued`
`"Nesting" if Statements`
• The inner `if` statement is evaluated only if the test condition of the outer `if` test first evaluates to `true`
• The following code shows an example of a nested if statement

#### Example Showing a Nested `if` Statement

 ```1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 ``` ```#include using namespace std; int main() { int guess = 0; cout << "I'm thinking of a number between" << " 1 and 10.\nCan you guess it?\n\n" << "Enter your guess: "; cin >> guess; if (guess != 7) { if (guess < 7) { cout << "Your guess is too low.\n"; } else { cout << "Your guess is too high.\n"; } } else { cout << "*** Correct! ***\n"; } return 0; } ```

#### Nesting in the `else` Clause

• You can also nest `if` statements in the `else` clause
• When used this way, the computer can make only one selection
• As soon as a condition is found to be true, the rest of the selections are ignored
• The following code shows an example of a nested `else if` statement
• For example:
```if (guess < 7)
{
cout << "Your guess is too low.\n";
}
else if (guess > 7)
{
cout << "Your guess is too high.\n";
}
else
{
cout << "*** Correct! ***\n";
}
```
• The trick in understanding this type of logic is to remember:
• You start at the top
• The computer makes only one selection
• Once the selection is made and processes, the computer skips the rest of the options

#### Example Showing a Nested `else if` Statement

 ```1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ``` ```#include using namespace std; int main() { int guess = 0; cout << "I'm thinking of a number between" << " 1 and 10.\nCan you guess it?\n\n" << "Enter your guess: "; cin >> guess; if (guess < 7) { cout << "Your guess is too low.\n"; } else if (guess > 7) { cout << "Your guess is too high.\n"; } else { cout << "*** Correct! ***\n"; } return 0; } ```

#### Programming Style: Indentation of if-else-if Statements

• Note the alignment of the nested statements below:
```if (guess < 7)
{
cout << "Your guess is too low.\n";
}
else
{
if (guess > 7)
{
cout << "Your guess is too high.\n";
}
else
{
cout << "*** Correct! ***\n";
}
}
```
• The above style is WRONG
```if (guess < 7)
{
cout << "Your guess is too low.\n";
}
else if (guess > 7)
{
cout << "Your guess is too high.\n";
}
else
{
cout << "*** Correct! ***\n";
}
```
• This shows more clearly that we are making a single choice among multiple alternatives
• Also, it prevents indentations from cascading to the right as we add more selections

### Multiple Alternatives

• By using collections of if-else statements, a program can distinguish between multiple alternatives

#### Choosing Between Alternatives

• This program has five alternatives to choose from:
1. "penny"
2. "nickel"
3. "dime"
4. "quarter"
5. erroneous input
• Note that the order that the alternatives are checked is unimportant
• We can follow the alternatives in the flowchart shown below

#### Flowchart of Multiple Alternative for Converting Coins to Values

• For example, consider the following example where a user enters the name of a coin, and the program displays the value:

#### Program Converting Coin Names to Values

 ```1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 ``` ```#include using namespace std; int main() { cout << "Enter coin name: "; string name; cin >> name; double value = 0; if (name == "penny") { value = 0.01; } else if (name == "nickel") { value = 0.05; } else if (name == "dime") { value = 0.10; } else if (name == "quarter") { value = 0.25; } else { cout << name << " is not a valid coin name\n"; } cout << "Value = " << value << "\n"; return 0; }```

#### Activity 9.1: Grades (10 pts)

We want to write a program to calculate a student's letter grade according to the following table:

greater than or equal to 90A
less than 90 but greater than or equal to 80B
less than 80 but greater than or equal to 70C
less than 70 but greater than or equal to 60D
less than 60F
• Find a partner for pair programming.
• Copy the following program into a text editor, save it as grader.cpp. Change the comments, then compile and run the starter program to make sure you copied it correctly.
`/*`
`* Name 1`
`* Name 2`
`* Section`
```*/#include <iostream>
using namespace std;

int main() {

return 0;
} ```

• Add code to get user input into a variable named `score` of type `double`. When you run the program after adding this code, the output should look like:
```Enter a score: 95.7
```

Make sure you declare the variable with a compatible data type. Note that the underlined numbers above shows what theuser enters and is not part of your code

• First we will look at a series of `if` statements and see that `if` statements alone are not enough to solve this problem. Copy the following into your program after the input statements:
``` ```

Compile and run your modified program. There is a logic problem with this code. Each test condition needs to work over a range of values rather than with a single value.

• One way to correct the problem is to nest an `if` statement inside of another `if` statement. To see how this works, modify your code to add nested `if` statements as shown below:

To test the range, the outer `if` statement tests the lower condition and the inner `if` statement tests the upper condition.

if (score >= 90)
{
}
if (score >= 80)
{
if (score < 90)
}
if (score >= 70)
{
if (score < 80)
}
if (score >= 60)
{
if (score < 70)
}
if (score < 60)
{
}
• What is another way to fix this problem that we have already seen?
• Perhaps the most elegant solution is to nest an `if` statement in the `else` clause of the preceding `if`. Modify the series of `if`statements to include an `else` clause as shown below:

if (score > = 90)
{
}
else if (score >= 80)
{
}
//you fill in the statements here
else
{
}

• We are nesting `if` statements in the `else` clause. Nesting in the `else` clause makes each test condition of the `if` statement exclusive of the others because each test condition eliminates all the preceding conditions.
• Compile and run your modified program to make sure you made the changes correctly. When you run the program, the output should look like:
```Enter a score: 80
B
```

Run your program a few times with different score to verify that any score displays the correct letter grade.

### When Order Matters

• In some cases, the order of the tests is important
• For example, look at the following program from the textbook that displays a description of the likely impact of an earthquake based on its magnitude on the Richter scale

#### Program Showing Multiple Alternatives Where Order Matters

 ```1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 ``` ```#include using namespace std; int main() { cout << "Enter a magnitude on the Richter scale: "; double richter; cin >> richter; if (richter >= 8.0) { cout << "Most structures fall\n"; } else if (richter >= 7.0) { cout << "Many buildings destroyed\n"; } else if (richter >= 6.0) { cout << "Many buildings considerably damaged, " << "some collapse\n"; } else if (richter >= 4.5) { cout << "Damage to poorly constructed buildings\n"; } else if (richter >= 3.5) { cout << "Felt by many people, no destruction\n"; } else if (richter >= 0) { cout << "Generally not felt by people\n"; } else { cout << "Negative numbers are not valid\n"; } return 0; } ```

#### Order Is Important

• Note that the order of the tests is important to ensure that the right results are printed
• If we rearranged the order of the `if-else` statements, we would get the wrong results
• For example, if we reversed the order of the tests:
```if (richter >= 0) { // tests in wrong order
cout << "Generally not felt by people\n";
} else if (richter >= 3.5) {
cout << "Felt by many people, no destruction\n";
} else if (richter >= 4.5) {
cout << "Damage to poorly constructed buildings\n";
} else if (richter >= 6.0) {
cout << "Many buildings considerably damaged, "
<< "some collapse\n";
} else if (richter >= 7.0) {
cout << "Many buildings destroyed\n";
} else if (richter >= 8.0) {
cout << "Most structures fall\n";
} else {
cout << "Negative numbers are not valid\n";
}
```
• This does not work because all values meet the first condition
• Every other test will never be attempted

#### Importance of Using `if-else-if` Structure

• Note that we cannot remove the `else` portion of the structure like shown below:
```if (richter >= 8.0) { // Does not use else
cout << "Most structures fall\n";
}
if (richter >= 7.0) {
cout << "Many buildings destroyed\n";
}
if (richter >= 6.0) {
cout << "Many buildings considerably damaged, "
<< "some collapse\n";
}
if (richter >= 4.5) {
cout << "Damage to poorly constructed buildings\n";
}
if (richter >= 3.5) {
cout << "Felt by many people, no destruction\n";
}
if (richter >= 0) {
cout << "Generally not felt by people\n";
}
if (richter < 0) {
cout << "Negative numbers are not valid\n";
}
```
• The conditions must be exclusive and we need the else-if conditions to ensure exclusivity
• Independent `if` statements may cause a single input to print several messages

### Wrap up

• Review today's learning objectives

### Midterm 1

Upcoming Assignments

• Assignment 9 due Thursday at 3:20pm
• Lab 5 due this Friday

~See You Thursday!~