liveBook · Manning

Case statement

As explained in Modern C

A switch statement is a C selection construct that chooses among multiple execution paths based on the value of an expression.

A case statement uses a switch expression to choose among several execution paths. It is especially useful when a cascade of if-else statements would be tedious. section 3.0 section 3.3

The switch expression determines the jump target. Each case and default label marks a target; execution continues from the selected label until break or the end of the block. section 3.3

TAKEAWAY: break terminates the enclosing switch and transfers control to the following statement. Without it, execution falls through into subsequent cases. section 3.3 section 3.4

Keep these language rules in view:

  • Use case values that are integer constant expressions, such as fixed values written directly in the source. section 3.3
  • Do not use variables as individual case values; variables belong in the switch expression. section 3.3
  • Use default for values matching no case label. section 3.3

Intentional fallthrough

A switch on count can begin at case 3 and run the statements for cases 3, 2, 1, and 0 when no break statements intervene, producing progressively shorter output.

How Modern C covers it

How deep it goes
Explains syntax, jump-target semantics, break termination, fallthrough, default, and integer constant case restrictions.
Examples
Uses character classification, puts calls, and a numeric fallthrough example that produces progressively shorter output.
What it assumes
Assumes basic C control flow and expressions while defining multiple selection, labels, break, default, and case restrictions.
What you can do after
Builds multiple-selection control flow with case, default, and break, and recognizes intentional or accidental fallthrough.
How it connects
Connects switch with if statements, break statements, jump targets, and integer constant expressions.

Also explained in

Compare how 9 books explain case statement →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Modern C. The section links point to the parts of the book it draws on.