Skip to main content
Contents
Dark Mode Prev Up Next Scratch ActiveCode Profile
\(
\newcommand{\lt}{<}
\newcommand{\gt}{>}
\newcommand{\amp}{&}
\definecolor{fillinmathshade}{gray}{0.9}
\newcommand{\fillinmath}[1]{\mathchoice{\colorbox{fillinmathshade}{$\displaystyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\textstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptscriptstyle\phantom{\,#1\,}$}}}
\)
Section 4.4 Chained Conditionals
Sometimes you want to check for a number of related conditions and choose one of several actions. One way to do this is by
chaining a series of ifs and elses:
Listing 4.4.1. The following program classifies a number (x) as positive, negative, or zero. Feel free to change the value of x to make sure it works.Try changing the value of x above to see how the output is impacted.
These chains can be as long as you want, although they can be difficult to read if they get out of hand. One way to make them easier to read is to use standard indentation, as demonstrated in these examples. If you keep all the statements and squiggly-braces lined up, you are less likely to make syntax errors and you can find them more quickly if you do.
Checkpoint 4.4.1 .
What will print after the following code is executed?
#include <iostream>
using namespace std;
int main() {
int x = 10;
if (x > 8) {
cout << "One! ";
}
if (x > 6) {
cout << "Two! ";
}
if (x > 3) {
cout << "Three!" << endl;
}
return 0;
}
Make note of the use of "if" instead of "else if" or "else".
Make note of the use of "if" instead of "else if" or "else".
Make note of the use of "if" instead of "else if" or "else".
When we have "if" statments, but no "else if" or "else", every condition will be checked.
Checkpoint 4.4.2 .
What will print after the following code is executed?
#include <iostream>
using namespace std;
int main() {
int x = 10;
if (x > 8) {
cout << "One! " ;
}
else if (x > 6) {
cout << "Two! ";
}
else {
cout << "Three!" << endl;
}
return 0;
}
Remember that only one action will be completed in a chain of "ifs", "else ifs", and "ifs"
The chain of "ifs", "else ifs", and "elses" results in only one action being completed.
Remember that a chain of "ifs", "else ifs", and "elses" will result in only one action being completed.
Remember that a chain of "ifs", "else ifs", and "elses" will result in only one action being completed.
Checkpoint 4.4.3 .
What will print after the following code is executed?
#include <iostream>
using namespace std;
int main() {
int x = 7;
if (x > 8) {
cout << "One! " ;
}
if (x > 6) {
cout << "Two! ";
}
if (x > 3) {
cout << "Three!" << endl;
}
return 0;
}
Make note of the use of "if" instead of "else if" or "else".
When we have "if" statments, but no "else if" or "else", every condition will be checked.
The first statement will not be executed because x > 8 is not true. Also, make note of the use of "if" instead of "else if" or "else".
The first statement will not be executed because x > 8 is not true.
Checkpoint 4.4.4 .
What will print after the following code is executed?
#include <iostream>
using namespace std;
int main() {
int x = 7;
if (x > 8) {
cout << "One! " ;
}
else if (x > 6) {
cout << "Two! ";
}
else {
cout << "Three!" << endl;
}
return 0;
}
Only one action will is completed in a chain of "ifs", "else ifs", and "ifs";
Remember that only one action will be completed in a chain of "ifs", "else ifs", and "ifs".
The first condition will not be satisfied. Also, a chain of "ifs", "else ifs", and "elses" will result in only one action being completed.
hge first condition will not be satisfied. Also, a chain of "ifs", "else ifs", and "elses" will result in only one action being completed.
You have attempted
of
activities on this page.