The "nested-if-else" VS "switch" Statement

The "nested-if-else" VS "switch" Statement

Both the nested "if-else" and switch statement are used for multiple selections but following are the differences between these statements.

Nested if-else StatementSwitch statement
1It becomes complicated for multiple selections.It is easy to understand for multiple selections.
2It uses an independent expression for each case.It uses a single expression for all cases, but each case must have a constant value of integer type or character type.
3The test condition can be given in a specified range of values. If the given condition matches then the statements under it will be executed. Only a single expression is given in the switch statement which returns a single value. The test condition cannot be given in a specified range. It is the major drawback of the switch statement.

Creative Commons License
Pakistan How by Tahira Aijaz is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.