what is the different between if-else and switch statment
(other than syntax)

Answers were Sorted based on User's Feedback



what is the different between if-else and switch statment (other than syntax)..

Answer / thiyagu.p

Simply saying, IF-ELSE can have values based on constraints
where SWITCH can have values based on user choice.

Is This Answer Correct ?    187 Yes 42 No

what is the different between if-else and switch statment (other than syntax)..

Answer / fazlur rahaman naik

The main difference between switch - case and if - else is
we can't compare variables.

in the if - else, first the condition is verified,then it
comes to else whereas in the switch - case first it checks
the cases and then it switches to that particular case.

Is This Answer Correct ?    168 Yes 35 No

what is the different between if-else and switch statment (other than syntax)..

Answer / kuldeep sadhu

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.............

Is This Answer Correct ?    61 Yes 23 No

what is the different between if-else and switch statment (other than syntax)..

Answer / sujith

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
tree.

I would appreciate analyzing the assembly code, with the
same source, with if else tree and switch case statements.

Is This Answer Correct ?    58 Yes 33 No

what is the different between if-else and switch statment (other than syntax)..

Answer / ananth kumar

switch has its own pros and cons,

Favours coding style.
From performance point, switch case creates table. It
directly jumps to the required location based on table
contents.

If-else is hard for code-walk
Optimised (please check).

Is This Answer Correct ?    44 Yes 21 No

what is the different between if-else and switch statment (other than syntax)..

Answer / vikky

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.

Is This Answer Correct ?    36 Yes 20 No

what is the different between if-else and switch statment (other than syntax)..

Answer / sanjay parmar sirsa

If-else
we depand the condition
if condition true then true block will be excuted
else
false block will be excuted
switch have number of choice we can excute any choice

Is This Answer Correct ?    21 Yes 6 No

what is the different between if-else and switch statment (other than syntax)..

Answer / mikew

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:

_jump_table:
DEFW case1_address
DEFW case1_value
...
DEFW caseN_address
DEFW caseN_value
DEFW NULL /* terminator */

- search _jump_table for case_value
- branch to corresponding case_address, or take default
action if NULL terminator found instead.

In the special case that case1 ... caseN values are
sequential numbers (maybe with a few gaps) then the table
and code can be further simplified as a simple indexed branch:

_jump_table:
DEFW case1_address
...
DEFW default_address /* fill in any holes !*/
...
DEFW caseN_address

- check value is between case1_value and caseN_value
- subtract case1_value
- load branch address word from _jump_table word-indexed on
previous result

Is This Answer Correct ?    27 Yes 14 No

what is the different between if-else and switch statment (other than syntax)..

Answer / sherin

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.

Is This Answer Correct ?    17 Yes 7 No

what is the different between if-else and switch statment (other than syntax)..

Answer / sujoy dutta

we can use all varriables within if else.but we can't use
all verriables within switch case.example :-float and
String we can't use within switch case.

Is This Answer Correct ?    17 Yes 7 No

Post New Answer

More C Interview Questions

sqrt(x+sqrt(x+sqrt(x+sqrt(x))))=2; Find the value of x?

4 Answers   Subex,


a single linked list consists of nodes a to z .print the nodes in reverse order from z to a using recursion

0 Answers   Mindteck,


How do I copy files?

0 Answers  


Tell us the difference between these two : #include"stdio.h" #include<stdio.h> define in detial.

5 Answers  


int a[3][5]={ {1,2,3,4,5],{2,3,4,5,6},{10,11,12,13,14}}; int *p=&a; printf(ā€œ%dā€,*(*(x+1)+3));

2 Answers   Wipro,






Explain what is the difference between a string and an array?

0 Answers  


to find out the reverse digit of a given number

6 Answers   Infosys, Microsoft, TCS, Wipro,


can we print any string in c language without using semicolon(;)(terminator) in whole program.

11 Answers  


What will be the result of the following C language program? main() { int a = 0; int b = 20; char x = 1; char y = 10; if(a,b,x,y) printf("Welcome"); }

1 Answers  


which of the following statements is incorrect a.typedef struct new{ int n1; char n2; } DATA; b.typedef struct { int n3; char *n4; }ICE; c.typedef union { int n5; float n6; } UDT; d.#typedef union { int n7; float n8; } TUDAT;

5 Answers   Assurgent, TCS,


why java is called as a purely oops language.

3 Answers   TVS,


Explain is it better to use a pointer to navigate an array of values, or is it better to use a subscripted array name?

0 Answers  


Categories