Back to questions
JuniorTheoryCommon
What does the program print?
#include <iostream>
bool is_small(int x) {
std::cout << x;
return x < 10;
}
int main() {
bool r = is_small(5) || is_small(20);
std::cout << ' ' << r << '\n';
}5 1
- Expecting
520 1:||evaluates its right operand only when the left one isfalse, andis_small(5)is alreadytrue. - Expecting
true: by defaultstd::coutprints aboolas1or0, not as a word.
- What would the program print if
||were replaced with&&? - How does short-circuit evaluation make
d != 0 && n / d > 2safe whendis zero?