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.............
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.
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:
There are some things that you simply cannot do with a
switch. These are:
A float expression cannot be tested using a switch
Cases can never have variable expressions (for example it is
wrong to say case a +3 : )
Multiple cases cannot use same expressions.
the factorial of non-negative integer n is written n! and
is defined as follows:
n!=n*(n-1)*(n-2)........1(for values of n greater than or
equal to 1 and
Perform the following
1.write a c program that reads a non-negative integer and
computes and prints its factorial.
2. write a C program that estimates the value of the
mathematical constant e by using the formula:
3. write a c program the computes the value ex by using the
A MobileNumber is a VIP number if it satisfy the following
The operator should be Vodafone.
Atleast one 0 (Zero) should be exist in mobile number.
The number should not end with 8.
The single digit sum of all the digits in the number should
be equal to 9. For example if the number is 9876543210, the
sum is 9+8+7+...+1+0 = 45. Sum of 4+5 = 9.
Write a method:
private boolean isVIPMobileNumber(String mobileNum, String
mobileNum phone number
operator mobile operator as bsnl, Vodafone