GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  Programming  >  C
Next Question 
 C  |  Question 1 of 453    Print  
When is a switch statement better than multiple if statements?
 
A switch statement is generally best to use when you have more than two conditional expressions based on a single variable of numeric type.



  
Total Answers and Comments: 25 Last Update: January 09, 2009   
  
 Sponsored Links

 
 Best Rated Answer
Submitted by: BHARATESH
 
In multiple if statements the conditions are to checked as many times the if statements are written where as in switch conditiion the condition is checked only once and jumps to required block

Above answer was rated as good by the following members:
tigersasi, sarojkumar21.panda
  Sorting Options  
  Page 1 of 3   « First    1    2    3    >     Last »  
September 18, 2005 08:52:38   #1  
Hirma        

RE: When is a switch statement better than...
Switch statement is useful when we are having multiple options for the same variable but when we have to use mutiple conditions simultaneously then it would be better to use if else.
 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    
October 06, 2005 15:31:36   #2  
Nagesh Kumar        

switch is bettera than...
The switch statement is better than multiple if statements when there are more than two alternatives to be selected whether the case value matches to the variable of either character or integer type.
 
Is this answer useful? Yes | No
November 28, 2005 01:03:21   #3  
nsivaramprasad Member Since: November 2005   Contribution: 6    

RE: When is a switch statement better than...
Both statements has their own purpose. If the comparision is between switch and if statements only (not if-else construct) Switch is better to use provided the results of the check-condition are discreet values. Switch statement gives more clarity in code than multiple if construct.
 
Is this answer useful? Yes | No
March 02, 2006 08:06:47   #4  
kiran kumar        

RE: When is a switch statement better than...
switch is used when we have multiple options for the same variable based on the condition any one of the case in the group will be executed
 
Is this answer useful? Yes | No
July 13, 2006 08:40:17   #5  
Syam Prasad N        

RE: When is a switch statement better than...
A switch statement is generally best to use when you have more than two conditional expressions based on a single variable of numeric type.
 
Is this answer useful? Yes | No
August 08, 2006 07:03:38   #6  
deep275 Member Since: April 2006   Contribution: 2    

RE: When is a switch statement better than...
The switch statement is a construct that is used when many conditions are being tested for. When there are many conditions it becomes too difficult and complicated to use the if and else if constructs. Nested if/else statements arise when there are multiple alternative paths of execution based on some condition that is being tested for.
 
Is this answer useful? Yes | No
October 10, 2006 07:08:55   #7  
datta.s.k        

RE: When is a switch statement better than...
In if statements to get required result we have to check many conditions first.this is time consuming.where as in switch statements we are switching to the result in less steps by case checking.
 
Is this answer useful? Yes | No
October 10, 2006 07:12:20   #8  
BHARATESH        

RE: When is a switch statement better than...
In multiple if statements the conditions are to checked as many times the if statements are written where as in switch conditiion the condition is checked only once and jumps to required block
 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    
May 01, 2007 08:36:16   #9  
k4kishore Member Since: May 2007   Contribution: 1    

RE: When is a switch statement better than...

Straight answer for this question is "When more than 2 conditions are there and condition variable is not a condition again (should be either character or integer)" If any thing is wong in this please let me know


 
Is this answer useful? Yes | No
June 08, 2007 09:22:46   #10  
Anu        

RE: When is a switch statement better than...
Switch is better when the equality condition comes into picture.Else if-else would work fine.
 
Is this answer useful? Yes | No
  Page 1 of 3   « First    1    2    3    >     Last »  


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape