switch statement


switch statement

(programming)(Or case statement, multi-way branch) Aconstruct found in most high-level languages for selectingone of several possible blocks of code or branch destinationsdepending 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 thewhole switch statemetnt. The lack of a break statement afterthe first case means that execution will fall through intothe second case. Since this is a common programming error youshould add a comment if it is intentional.

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

A similar construct in some functional languages returns thevalue of one of several expressions selected according to thevalue of the first expression. A distant relation to themodern switch statement is Fortran's computed goto.