C++ Language/ProgramFlow/BreakStatement/FallthroughInSwitch

From Wikibooks, open books for an open world
Jump to navigation Jump to search

A switch statement is an efficient substitute for many if (x==1) {body} else if (x==5) {body} else if (x==99) {body} chains. In a switch statement, each body is a sequence of statements preceded by a case 5: label. If you don't terminate that sequence of statements by break;, then program flow will fall-through and also perform the next sequence of statements. This is often a programming bug, so when you intentionally do this, write [[fallthrough]]; in place of break;.

Additional information about fallthrough (includes interactive examples)