Encyclopedia

switch statement

Also found in: Wikipedia.

switch statement

(programming)
(Or case statement, multi-way branch) A construct found in most high-level languages for selecting one of several possible blocks of code or branch destinations depending on the value of an expression. An example in C is

switch (foo(x, y)) case 1: printf

The break statements cause execution to continue after the whole switch statemetnt. The lack of a break statement after the first case means that execution will fall through into the second case. Since this is a common programming error you should add a comment if it is intentional.

If none of the explicit cases matches the expression value then the (optional) default case is taken.

A similar construct in some functional languages returns the value of one of several expressions selected according to the value of the first expression. A distant relation to the modern switch statement is Fortran's computed goto.
This article is provided by FOLDOC - Free Online Dictionary of Computing (foldoc.org)
Mentioned in
References in periodicals archive
In C# a switch statement may not "fall through" to the next statement if it does any work.
Trap #9: You may not "fall through" in switch statements.
The semantics of the switch statement are as follows: The switch statement transfers control to one of several statements depending on the value of the expression <expr>.
The acyclic execution path complexity for the switch statement is
A set of small language changes intended to simplify common, day-to-day programming tasks: Strings in switch statements, try-with-resources statements, improved type inference for generic instance creation ("diamond"), simplified varargs method invocation, better integral literals, and improved exception handling (multi-catch)
Copyright © 2003-2025 Farlex, Inc Disclaimer
All content on this website, including dictionary, thesaurus, literature, geography, and other reference data is for informational purposes only. This information should not be considered complete, up to date, and is not intended to be used in place of a visit, consultation, or advice of a legal, medical, or any other professional.