Case Statement
. switch expression
begin
case value: statement
case value: statement
..
case value: statement
default: statement
end
.evaluate the expression
. find which value in the list of cases is the same as the value of the expression
.
- Default value matches the expression if none of the
values explicitly mentioned in the cases matches the expression
. execute the statement associated with the value found
There is a selector expression, which is to be evaluated, followed by n constant values that the expression can take. This may also include a default value which always matches the expression if no other value does.
The intended translation of a switch case code to:
.evaluate the expression
. find which value in the list of cases is the same as the value of the expression.
. Default value matches the expression if none of the values explicitly mentioned in the cases matches the expression
. execute the statement associated with the value found
Most machines provide instruction in hardware such that case instruction can be implemented easily. So, case is treated differently and not as a combination of if-then statements.
|