Checkpoint 5.7.1.
Multiple Response: How could you re-write the following code using a single conditional?
if (x > 0) {
if (x < 10) {
cout << "x is a positive single digit" << endl;
}
}
- if (x > 0 && x < 10) {...
- This is exactly what the nested conditionals are saying.
- if (x > 0 || x < 10) {...
- || represents "or", but we need both sides of the conditional to be true.
- if (x > 0 ! x < 10) {...
- The ! operator cannot be used to compare two sides of a conditional.
- if ( !(x < 0) && !(x > 10) ) {...
- If x = 0 or if x = 10, this expression will return true when it shouldn’t.
- if ( !(x <= 0) && !(x >= 10) ) {...
- If it IS NOT what we don’t want, then it IS what we want!