Conditional Structures in C. if-else,switch,while and for. | Page 3

STRUCTURE CONDITIONAL

switch

A switch statement (multiple alternative) allows to select, by means of an expression, the next block of instructions to execute among several possible ones. In C language, the syntax is used to write a multiple alternative instruction (switch):

switch (<expression>) {

case <expression_1>: [<block_of_instructions_1>]

[break; ]

case <expression_2>: [<block_of_instructions_2>]

[break; ]

case <n_expression: [<n_instructions_block>]

[break; ]

[default: <block_of_instructions_n + 1>]

}

The result of evaluating the <expression> must be a value belonging to a finite and ordered type of data, that is, integer, logical, character, enumerated or subrange.