All topics
Conditions
if/else, the dangling else, if with an initializer, switch and fall-through.
Sort:
2 questions
JuniorTheoryCommonWhat does the program print?
JuniorTheoryCommon
What does the program print?
bcd
Open full question →Common mistakes
- ✗Expecting
b: acaselabel does not end the code above it, so withoutbreakexecution runs on intocase 3and further. - ✗Expecting
bc: falling through does not stop atdefault, which is a label like anycase, sodis printed too.
Follow-up questions
- →Does
[[fallthrough]]change what this program prints, and why would you add it? - →Why may a variable declared right after a
caselabel, without braces, cause a compile error?
JuniorTheoryOccasionalWhat does the program print?
JuniorTheoryOccasional
What does the program print?
-1 1
Open full question →Common mistakes
- ✗Expecting
1 -1, as the indentation suggests: anelsebelongs to the nearestifwithout anelse, hereif (x > 10), and indentation changes nothing. - ✗Expecting a compile error: the nearest-
ifrule leaves no ambiguity, so Clang and GCC only warn and suggest braces.
Follow-up questions
- →How would you place braces in
kindso that it returns-1for everyx <= 0? - →Which compiler warning flags an ambiguous
else, and what fix does it suggest?