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 Statement1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
| #include <iostream>
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 ClauseExample Showing a Nested else if Statement1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
| #include <iostream>
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
- Instead, we use:
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:
- "penny"
- "nickel"
- "dime"
- "quarter"
- 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 Values1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
| #include <iostream>
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: Numerical Grade | Letter Grade |
---|
greater than or equal to 90 | A | less than 90 but greater than or equal to 80 | B | less than 80 but greater than or equal to 70 | C | less than 70 but greater than or equal to 60 | D | less than 60 | F |
- 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() {
// Enter your code here
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.
string grade;
if (score >= 90) {
grade = "A";
}
if (score >= 80) {
if (score < 90)
grade = "B";
}
if (score >= 70) {
if (score < 80)
grade = "C";
}
if (score >= 60) {
if (score < 70)
grade = "D";
}
if (score < 60) {
grade = "F";
}
cout << grade << endl;
- 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:
string grade;
if (score > = 90) {
grade = "A";
}
else if (score >= 80) {
grade = "B";
}
//you fill in the statements here
else {
grade = "F"; }
cout << grade << endl;
- 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.
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 Matters1
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 <iostream>
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 1Upcoming Assignments
- Assignment 9 due Thursday at 3:20pm
- Lab 5 due this Friday
~See You Thursday!~ |
|