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:
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.
Note
If you have adjacent if
statements, the program will go through
executing each conditional, regardless if the conditions are met.
However, as soon as you add an else
or even an else if
statement,
the program will stop executing the chained conditionals as soon as a
condition is met.
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.
- Three!
- Make note of the use of "if" instead of "else if" or "else".
- One!
- Make note of the use of "if" instead of "else if" or "else".
- One! Two!
- Make note of the use of "if" instead of "else if" or "else".
- One! Two! Three!
- When we have "if" statments, but no "else if" or "else", every condition will be checked.
Q-2: What will print after the following code is executed?
#include <iostream>
using std::cout;
int main () {
int x = 10;
if (x > 8) {
cout << "One! ";
}
if (x > 6) {
cout << "Two! ";
}
if (x > 3) {
cout << "Three!\n";
}
return 0;
}
- Three!
- Remember that only one action will be completed in a chain of "ifs", "else ifs", and "ifs"
- One!
- The chain of "ifs", "else ifs", and "elses" results in only one action being completed.
- One! Two!
- Remember that a chain of "ifs", "else ifs", and "elses" will result in only one action being completed.
- One! Two! Three!
- Remember that a chain of "ifs", "else ifs", and "elses" will result in only one action being completed.
Q-3: What will print after the following code is executed?
#include <iostream>
using std::cout;
int main () {
int x = 10;
if (x > 8)
{
cout << "One! " ;
}
else if (x > 6)
{
cout << "Two! ";
}
else
{
cout << "Three!\n";
}
return 0;
}
- Two!
- Make note of the use of "if" instead of "else if" or "else".
- Two! Three!
- When we have "if" statments, but no "else if" or "else", every condition will be checked.
- One! Two!
- 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".
- One! Two! Three!
- The first statement will not be executed because x > 8 is not true.
Q-4: What will print after the following code is executed?
#include <iostream>
int main () {
int x = 7;
if (x > 8) {
std::cout << "One! " ;
}
if (x > 6) {
std::cout << "Two! ";
}
if (x > 3) {
std::cout << "Three!\n";
}
return 0;
}
- Two!
- Only one action will is completed in a chain of "ifs", "else ifs", and "ifs";
- Two! Three!
- Remember that only one action will be completed in a chain of "ifs", "else ifs", and "ifs".
- One! Two!
- The first condition will not be satisfied. Also, a chain of "ifs", "else ifs", and "elses" will result in only one action being completed.
- One! Two! Three!
- hge first condition will not be satisfied. Also, a chain of "ifs", "else ifs", and "elses" will result in only one action being completed.
Q-5: What will print after the following code is executed?
#include <iostream>
int main () {
int x = 7;
if (x > 8)
{
std::cout << "One! " ;
}
else if (x > 6)
{
std::cout << "Two! ";
}
else
{
std::cout << "Three!\n";
}
return 0;
}
More to Explore
if and comparison operators from cppreference