Case statement
A case or switch construct is a conditional control-flow construct that selects a branch or result from alternatives by matching a value or testing conditions.
Learn Go with Pocket-Sized Projects
A switch statement selects among multiple execution branches by evaluating an expression against case conditions.
- How it is used
- Selects language-specific greetings, cache-update actions, and channel responses through readable alternatives.
- How deep it goes
- Explains matching, default handling, implicit breaks, first-case selection, and optional fallthrough without descending into compiler internals.
- Examples
- Demonstrates language greetings, bounded-cache upserts, and channel cancellation in a concurrent maze solver.
- What you can do after
- Build it from scratch in Go to replace repetitive conditional chains with explicit multi-branch logic.
Read how Learn Go with Pocket-Sized Projects explains case statement →
The Well-Grounded Rubyist, Fourth Edition
A Ruby case statement is a conditional expression that selects execution based on matching a value against alternatives.
- How it is used
- Organizes conditional execution when one value must be compared with several possible alternatives.
- How deep it goes
- Explains syntax, first-match behavior, multiple alternatives, return values, condition-only forms, and internal === dispatch.
- Examples
- Uses user-input prompts, yes-or-no branching, regular-expression input matching, and Ticket objects with custom venue comparison.
- What you can do after
- Apply it via Ruby syntax to branch on values, combine alternatives, return selected expressions, and customize matching with ===.
Read how The Well-Grounded Rubyist, Fourth Edition explains case statement →
Clojure: The Essential Reference
A switch statement is a control-flow construct that selects an execution branch by matching a value against alternative cases.
- How it is used
- Selects a branch quickly when an expression must be matched against fixed alternatives.
- How deep it goes
- Defines inputs, results, exceptions, and literal-test restrictions, then inspects macro expansion and JVM bytecode.
- Examples
- Uses a calculator, quoted-symbol pitfalls, keystroke scoring, and a `cond` benchmark.
- What you can do after
- Apply it in Clojure to dispatch on literal values while avoiding unevaluated-test and duplicate-constant pitfalls.
Read how Clojure: The Essential Reference explains case statement →
100 Java Mistakes and How to Avoid Them
A switch statement is a Java control-flow statement that selects and executes a branch based on the value of an expression.
- How it is used
- Uses case branches to select control flow from a switch expression.
- How deep it goes
- Explains fall-through, prevention patterns, modern syntax, and static-analysis support, while deferring general switch semantics.
- Examples
- Shows button actions, intentional fall-through, character-to-Boolean conversion, and enum-based weekend selection.
- What you can do after
- Diagnose accidental fall-through, write non-falling-through branches, and configure analyzers to recognize intentional flow.
Read how 100 Java Mistakes and How to Avoid Them explains case statement →
Kafka Streams in Action, Second Edition
A CASE statement is a SQL conditional expression that evaluates conditions and returns a corresponding result.
- How it is used
- Uses conditional branches in ksqlDB queries to derive values from fields in joined event streams.
- How deep it goes
- Explains branch evaluation through a reward-tier query, without detailing broader SQL expression semantics.
- Examples
- Demonstrates reward-point tiers based on store purchase totals within a customer-rewards stream.
- What you can do after
- Apply it via ksqlDB to derive categorized or tiered output fields from streaming query data.
Read how Kafka Streams in Action, Second Edition explains case statement →
Modern C, Third Edition
A switch statement is a C selection statement that transfers control to a matching case or default label based on an expression's value.
- How it is used
- Organizes multiple outcomes when cascaded if-else constructs would be tedious.
- How deep it goes
- Explains selection, jump targets, fall-through, constraints, and declaration hazards, while deferring broader control-flow mechanics.
- Examples
- Uses corvid selection, a count-generated triangle, and character dispatch in a recursive code-indentation parser.
- What you can do after
- Apply switch statements with deliberate fall-through, unique constant cases, default handling, and correctly terminated branches.
D3.js in Action, Third Edition
A switch statement selects and executes code based on which case matches an expression's value.
- How it is used
- Uses conditional branches to choose responsive SVG dimensions and grid columns from the browser width.
- How deep it goes
- Demonstrates `switch (true)` with ranges, `break`, `default`, and Svelte reactivity, without discussing language internals.
- Examples
- Uses screen-width breakpoints to size an SVG and arrange a Van Gogh visualization grid into one, two, or three columns.
- What you can do after
- Apply it via Svelte to recalculate SVG dimensions and layout choices whenever the browser width changes.
Read how D3.js in Action, Third Edition explains case statement →
Data-Oriented Programming: Reduce software complexity
A switch statement selects and executes code according to the value of an expression or discriminant.
- How it is used
- Contrasts switch-based data dispatch with object-oriented polymorphism.
- How deep it goes
- Demonstrates type dispatch, validation, and maintenance trade-offs, while stopping short of general switch semantics.
- Examples
- Uses dog, cat, and cow maps whose greetings are selected by their type fields.
- What you can do after
- Evaluate centralized case dispatch against separate polymorphic implementations and add validation for map-shaped inputs.
Read how Data-Oriented Programming: Reduce software complexity explains case statement →
Modern C
A switch statement is a C selection construct that chooses among multiple execution paths based on the value of an expression.
- How it is used
- Performs multiple selection when a Boolean if decision or an if-else cascade is insufficient.
- 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 you can do after
- Builds multiple-selection control flow with case, default, and break, and recognizes intentional or accidental fallthrough.
Related concepts
- conditional logicalternative to · Conditional logic is a programming technique that selects execution paths, actions, or result values according to evaluated conditions.
- break statementuses · A break statement terminates the nearest supported enclosing loop, switch, or similar control construct and transfers control to the following statement.
- when expressionalternative to · A when expression is a Kotlin multi-branch conditional expression that selects a result by matching conditions or a subject value against alternatives.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.








