C - Compare switch and nested if-else
1. The main difference between switch and if-else is that the expression
in if-else can result to a value of any data type, while the expression in
switch should result to an integer value or to a value of datatype char.
2. The second difference is that the switch statement can check only for
equality while the if-else statement can check any condition like equal
to, less than, greater than or equal to etc.
3. In a practical sense, the switch statement may be thought of as an
alternative to the use of nested if-else statements or else-if ladder.
However, it can only replace those if-else statements that test for
equality. If many such equality tests are to be carried out, then instead
of using complex nesting of if-else or else-if ladder it is more convenient to use the switch statement.
- Print Page