Checkpoint 5.8.1.
- (x % 2 == 1 && x == 7)
- 0
- (x % 2 == 0 || x + 1 == 4)
- 1
Match the conditional statement to its output, assuming it is outputted using cout and x = 3.
Try again!
bool isSingleDigit(int x) {
if (x >= 0 && x < 10) {
return true;
}
else {
return false;
}
}
isSingleDigit
. It is common to give boolean functions names that sound like yes/no questions. The return type is bool, which means that every return statement has to provide a bool expression.
x >= 0 && x < 10
has type amp; amp; bool, so there is nothing wrong with returning it directly, and avoiding the if statement altogether:
bool isSingleDigit(int x) {
return (x >= 0 && x < 10);
}
cout << isSingleDigit(2) << endl;
bool bigFlag = !isSingleDigit(17);
if (isSingleDigit(x)) {
cout << "x is little" << endl;
}
else {
cout << "x is big" << endl;
}