Switch-Case Statement
The Switch-Case Statement is a control structure that can be used in place of a set of nested if, else-if statements, under certain conditions.
Test Condition
In situations where a set of if, if-else statements are each testing to check the value of a single variable, and then executing specific statements when the test value matches the a valid value, then a Switch-Case statement can be used. This is often the case when using code that implements logic for a Finite State Machine, where the system is always in one state, out of a fixed set of possible states.
In the code below, in every conditional-test, we're always comparing a single value: testCondition. We are testing to see if it is equal to one of the following values: value1, value2, value3.
Fixed Set of Integral Values
The testCondition and acceptible set of values must be of Integral Data Types
: These include: integer, char, bool.
switch-case cannot be used with: floating-point / fractional values or object data-types.
As of Java 7, switch-case can be used with string values:
Last updated