Switch Case Statement
Switch Case Statement
Switch is a multiple-branch selection statement, which successively tests the value of an expression against a list of integer or character constants (floating point expression, for example, are not allowed). When a match is found, the statements associated with that constant are executed.
Syntax:
switch(expression) { case constant: statement; break; case constant: statement; break; .. .. default: statement; }
The value of the expression is tested against the constants specified in the case statements in a top-down order..
When a match is found, the statement sequence associated with that case is executed until the break statement or the end of the switch statement is reached.
When break is encountered in a switch, program execution "jumps" to the line of code following the switch statement.
The default statement is executed if no matches are found.
The default is optional.
The switch differs from the if in that switch can only test for equality, whereas if can evaluate any type of relational or logical expression.
No two case constants in the same switch can have identical values