Switch statement
A switch statement is a control-flow construct that selects an execution branch by matching an expression's value against alternative cases.
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 switch statement →
Type-Driven Development with Idris
An expression that evaluates a value and selects a result by matching it against alternatives.
- How it is used
- Uses case expressions to select function results and compute types from values.
- How deep it goes
- Works through interactive insertion, case splitting, hole inspection, and branch completion in function bodies and types.
- Examples
- Demonstrates vector insertion, a Boolean-dependent conversion function, and an error-checking door protocol.
- What you can do after
- Apply case expressions using Idris interactive editing to branch on intermediate results and refine dependent types.
Read how Type-Driven Development with Idris explains switch 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 switch 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 switch 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 switch 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 switch 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.
Read how Modern C, Third Edition explains switch statement →
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 switch 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 switch 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.
Systems Programming with Zig
A switch statement is a control-flow construct that selects code to execute based on an expression's matching case.
- How it is used
- Uses switches to decode CHIP-8 instructions and dispatch behavior for distinct enum options.
- How deep it goes
- Demonstrates nibble-based dispatch and enum exhaustiveness, without detailing switch implementation internals.
- Examples
- Shows a CHIP-8 case setting the I register and enum cases selecting arithmetic operations.
- What you can do after
- Apply Zig switches to decode CHIP-8 instructions or dispatch enum-specific behavior.
Read how Systems Programming with Zig explains switch statement →
Also covered in
- DuckDB in Action
- Learn SQL in a Month of Lunches
- Elixir in Action, Third Edition
- The Art of Code
- Build Financial Software with Generative AI (From Scratch)
- Learn Haskell by Example
- Snowflake Data Engineering
- Data-Oriented Programming in JavaIn early access (MEAP)
- Pro ASP.NET Core 7, Tenth Edition
- Learn PowerShell Scripting in a Month of Lunches, Second Edition: Write and organize scripts and tools
- Go by Example
What to know first
- conditional logicConditional logic is a programming technique that evaluates conditions to select among execution paths, actions, or result values.
- enumAn enum is a type whose values are selected from a finite set of named alternatives.
- contractA contract is an agreed specification of the conditions, expectations, rights, or obligations that govern behavior or interactions between parties or software components.
- threadA thread is a distinct sequence of execution with its own control flow and state, managed or scheduled by a processor, runtime, or operating system.
Related concepts
- with constructcontrasts with · The Idris with construct allows a function to pattern-match on an intermediate expression alongside its original inputs.
- conditional logicalternative to · Conditional logic is a programming technique that evaluates conditions to select among execution paths, actions, or result values.
- break statementuses · A break statement terminates the nearest supported enclosing loop, switch, or similar control construct and transfers control to the following statement.
- pattern matchingused by · Pattern matching is the process of comparing values or input with specified patterns to identify matches, classify data, or select corresponding behavior.
Questions about switch statement
What is switch statement?
A switch statement is a control-flow construct that selects an execution branch by matching an expression's value against alternative cases.
Which Manning books explain switch statement?
Learn Go with Pocket-Sized Projects, Type-Driven Development with Idris, The Well-Grounded Rubyist, Fourth Edition, Clojure: The Essential Reference, 100 Java Mistakes and How to Avoid Them, Kafka Streams in Action, Second Edition, Modern C, Third Edition, D3.js in Action, Third Edition and 3 more.
What should I know before learning switch statement?
Manning books that teach switch statement usually introduce conditional logic, enum, contract and thread first.
What is the difference between switch statement and with construct?
A switch statement is a control-flow construct that selects an execution branch by matching an expression's value against alternative cases. The Idris with construct allows a function to pattern-match on an intermediate expression alongside its original inputs.
What are the parts of switch statement?
Switch statement includes case label.
What is switch statement also called?
Switch statement is also called case statement.
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.










