int x = 2; switch (x) { case 1: puts("a"); case 2: puts("b"); case 3: puts("c"); } What is printed?
Correct answer
D. b then c, because execution continues into the later labels without any break
13 views
D. b then c, because execution continues into the later labels without any break
13 views
Switch fall-through means control enters at the matching label and keeps executing subsequent statements until a break, a return or the end of the switch, so x equal to 2 prints b and then c. Grouping several labels over one block is idiomatic, but a forgotten break remains a classic source of silent bugs. The compiler warning -Wimplicit-fallthrough catches most cases.
Asked in: NTS, PPSC IT, Campus Interviews
Not sure why the answer is D? Ask here and get an explanation.