ALLInterview.com :: Home Page            
 Advertise your Business Here     
Browse  |   Placement Papers  |   Company  |   Code Snippets  |   Certifications  |   Visa Questions
Post Question  |   Post Answer  |   My Panel  |   Search  |   Articles  |   Topics  |   ERRORS new
   Refer this Site  Refer This Site to Your Friends  Site Map  Bookmark this Site  Set it as your HomePage  Contact Us     Login  |  Sign Up                      
Google
   
 
Categories >> Software >> Programming Languages >> C
 
 


 

 
 C interview questions  C Interview Questions (2248)
 C++ interview questions  C++ Interview Questions (1106)
 VC++ interview questions  VC++ Interview Questions (342)
 Delphi interview questions  Delphi Interview Questions (510)
 Programming Languages AllOther interview questions  Programming Languages AllOther Interview Questions (644)
Question
what is the different between if-else and switch statment
(other than syntax)
 Question Submitted By :: Mytheensha
I also faced this Question!!     Rank Answer Posted By  
 
  Re: what is the different between if-else and switch statment (other than syntax)
Answer
# 1
Simply saying, IF-ELSE can have values based on constraints
where SWITCH can have values based on user choice.
 
Is This Answer Correct ?    139 Yes 29 No
Thiyagu.p
 
  Re: what is the different between if-else and switch statment (other than syntax)
Answer
# 2
Basically while coding developers prefer Switch rather than
If-Else.
But inturn switch gets transferred to IF-ELSe format during
compilation.
 
Is This Answer Correct ?    30 Yes 85 No
Subbu
 
 
 
  Re: what is the different between if-else and switch statment (other than syntax)
Answer
# 3
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 ?    123 Yes 26 No
Fazlur Rahaman Naik
 
  Re: what is the different between if-else and switch statment (other than syntax)
Answer
# 4
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 ?    49 Yes 27 No
Sujith
 
  Re: what is the different between if-else and switch statment (other than syntax)
Answer
# 5
Internally IF-ELSE implements Linear search, where as
SWITCH implements Binary search.
 
Is This Answer Correct ?    35 Yes 51 No
Srinivasa Reddy
 
  Re: what is the different between if-else and switch statment (other than syntax)
Answer
# 6
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
logical expressions.
we can also use switch statement to somple
comparisons ,though it not fair as following segments :
switch(a>b)
{
case 0 :
max=a;
break;
case 1 :
max=b;
break;
}
I want to conclude that if...else structure is more
flexible and reliable than switch statement.
 
Is This Answer Correct ?    29 Yes 25 No
Mathiyazhagan
 
  Re: what is the different between if-else and switch statment (other than syntax)
Answer
# 7
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 ?    33 Yes 16 No
Ananth Kumar
 
  Re: what is the different between if-else and switch statment (other than syntax)
Answer
# 8
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 ?    30 Yes 18 No
Vikky
 
  Re: what is the different between if-else and switch statment (other than syntax)
Answer
# 9
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 ?    21 Yes 11 No
Mikew
 
  Re: what is the different between if-else and switch statment (other than syntax)
Answer
# 10
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 ?    40 Yes 13 No
Kuldeep Sadhu
 

 
 
 
Other C Interview Questions
 
  Question Asked @ Answers
 
Write a function that accepts a sentence as a parameter, and returns the same with each of its words reversed. The returned sentence should have 1 blank space between each pair of words. Demonstrate the usage of this function from a main program. Example: Parameter: “jack and jill went up a hill” Return Value: “kcaj dna llij tnew pu a llih” Mind-Tree 4
write a program to delete an item from a particular location of an linear array?   1
write a pgm to print 1 1 2 1 1 2 3 2 1 1 2 3 4 3 2 1   3
find second largest element in array w/o using sorting techniques? use onle one for loop. Zycus-Infotech 15
write a C program, given number is double without using addt ion and multiplication operator?ex:n=6,ans=12,pls send me ans to goviseenu@gmail.com   5
write a C code To reverse a linked list Motorola 2
what r the cpu registers r ther?   1
Write a c code segment using a for loop that calculates and prints the sum of the even integers from 2 to 30, inclusive?   4
What is the difference between getch() and getche()?   1
How to run c Program without using IDE of c. means if program made in notepad.then how to compile by command prompt. TCS 1
What are .h files and what should I put in them?   3
f=(x>y)?x:y a) f points to max of x and y b) f points to min of x and y c)error HCL 4
 
For more C Interview Questions Click Here 
 
 
 
 
 


   
Copyright Policy  |  Terms of Service  |  Articles  |  Site Map  |  RSS Site Map  |  Contact Us
   
Copyright © 2013  ALLInterview.com.  All Rights Reserved.

ALLInterview.com   ::  KalAajKal.com