I would like to answer this question from a compiler
perspective. When we have if else or if else tree, we have
many compare instructions ( assembly generated by compiler)
where as switch has only one compare and jump instruction.
If the idea is to does something after comparing the values,
it is always better to go with the switch case than if else
I would appreciate analyzing the assembly code, with the
same source, with if else tree and switch case statements.
Though both are Braching statements,The selection of
statement is depending on developers.for simple
comparisions,there is no need to SWITCH statement.switch
statement will not use to compare floating,String and
we can also use switch statement to somple
comparisons ,though it not fair as following segments :
case 0 :
case 1 :
I want to conclude that if...else structure is more
flexible and reliable than switch statement.
expressions cannot be used as arguments in switsh, but in
if else any kind of exp can be used...
in the case of checkin a single variable for several values
SWITCH is the BEST.
in all other cases if else holds gud.
The switch branches on one value only, whereas the if-else
tests multiple logical expressions.
So you could say that the switch is a subset of if-else.
The potential difference if that switch is conceptually an
N-way branch point, whereas the if-else is always a
(repeated) binary branch.
However, if you are checking, say, a return code, against a
varied list of possibilities, then the switch can give
greater clarity to source code - and allow simpler addition
of new cases, making it easier for maintenance, as well as
allowing the compiler to generate simpler code.
Simpler code is possible because it can generate a jump
table to perform the multiple comparisons, i.e. in pseudo-code:
actually if else is a cndition checking system but the
switch is a selection of user typed type......
& secondly switch does not check all cases but jump directly
on the user interested case but if checkes all.............
read the folllowing code
# define MAX 100
# define MIN 100
if the initial value of x=200,what is the vlaue after
executing this code?
Result of the following program is