GeekInterview.com
Series: Subject: Topic:
Question: 587 of 587

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.
Asked by: Interview Candidate | Asked on: Mar 6th, 2005
Showing Answers 1 - 28 of 28 Answers
Hirma

Answered On : Sep 18th, 2005

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.

Yes  2 Users have rated as useful.
  
Login to rate this answer.
Nagesh Kumar

Answered On : Oct 6th, 2005

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.

  
Login to rate this answer.
nsivaramprasad

Answered On : Nov 28th, 2005

View all answers by nsivaramprasad

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.

  
Login to rate this answer.
kiran kumar

Answered On : Mar 2nd, 2006

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

  
Login to rate this answer.
Syam Prasad N

Answered On : Jul 13th, 2006

A switch statement is generally best to use when you have more than two conditional expressions based on a single variable ofnumeric type.

  
Login to rate this answer.
deep275

Answered On : Aug 8th, 2006

View all answers by deep275

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.

Yes  1 User has rated as useful.
  
Login to rate this answer.
datta.s.k

Answered On : Oct 10th, 2006

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.

  
Login to rate this answer.
BHARATESH

Answered On : Oct 10th, 2006

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

Yes  3 Users have rated as useful.
  
Login to rate this answer.
k4kishore

Answered On : May 1st, 2007

View all answers by k4kishore

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

  
Login to rate this answer.
Anu

Answered On : Jun 8th, 2007

Switch is better when the equality condition comes into picture.Else if-else would work fine.

  
Login to rate this answer.
Ajay

Answered On : Jun 9th, 2007

Actually, one can say CASE statement is definitely better when there are more than 2 conditions because it makes the code more structured, systematic and easy to understand. In terms of programming efficiency, it might not have an edge because I guess internally it will take the SWITCH statement cases as if-the-elses only.

Yes  1 User has rated as useful.
  
Login to rate this answer.
vbalaji_ece

Answered On : Jun 11th, 2007

View all answers by vbalaji_ece

Its not possible to use switch when our cases are floating points or strings. We cannot use them as cases directly. Anyway we can map them to integers and use which is an additional burden.
Bala'G'

  
Login to rate this answer.
Anil Patel

Answered On : Jul 12th, 2007

Switch statement is better when you have all similier kind of condition that you want to check. While in case of ladder if else non similier kind of condition is being tested.

  
Login to rate this answer.
M.Sandeep Bharadwaj

Answered On : Jul 15th, 2007

The disadvantage of switch statement is that we cannot have a case like case i<=20
Only ints & chars are allowed. Even float is not allowed

  
Login to rate this answer.
aditya

Answered On : Jul 26th, 2007

If you have multiple options for a single variable then you canprefer switch to if-else statement.

  
Login to rate this answer.
chandraprabha

Answered On : Aug 5th, 2007

A switch statement is better than if statement when we have number of options then it is better to use if statement, multiple if statement. eg: To check whether a givenletter isvowel or consonant.

  
Login to rate this answer.
Amit

Answered On : Aug 6th, 2007

[1] More Systematic Code - Great readability

[2] Where only one option is supposed to correct more often.

  
Login to rate this answer.
AKHILESH SINGH

Answered On : Aug 10th, 2007

when more than one statements in the program ,& requirements is invoke one statement at a time,in this situation we can use switch statement.

  
Login to rate this answer.

switch statement is used as it is better to use insteead of nested if and it is also increases readability of program

  
Login to rate this answer.
shailendra singh

Answered On : Aug 27th, 2007

Switch is nothing but the removal of the complexities that can be caused of normal if-else or the nested one.

Yes  1 User has rated as useful.
  
Login to rate this answer.
hari krishna

Answered On : Sep 1st, 2007

Your answer is good, but in my opinion switch statement is better than multiple if-else statements when we are using same variable in the case of multiple conditional expressions. By this we can decrease our code length.

  
Login to rate this answer.
ajay

Answered On : Sep 6th, 2007

Switch statement take a value as argument and go to a particular case statementand execute that.
In case of if else it check for each and every statement until did not get true value so it save the CPU processing time.

  
Login to rate this answer.
visitnitya

Answered On : Oct 6th, 2007

View all answers by visitnitya

when we have only single  condition then switch is better.otherwise.......

  
Login to rate this answer.
dinesh_27

Answered On : Sep 19th, 2008

View all answers by dinesh_27

Incase of if else, the compiler need to check the condition again and again
In case of switch the control directly jumps to the correct case label, so it makes it faster.

  
Login to rate this answer.
crimola

Answered On : Jan 8th, 2009

View all answers by crimola

Also, a modern C optimizing compiler will generate the same binary for multiple "if" statements as opposed to a "switch" statement.  However, code that uses "switch" for multiple numeric (symbolically speaking) comparisons is easier to read and follow its path.

  
Login to rate this answer.

Switch statements have its best uses hen there are more conditions because as we know that if statements checks for all the conditions and then when the condition is matched then the following body inside it is executed but this is not the case with sitch condition as it directly checks for the compatible conditions and execute the code.

  
Login to rate this answer.

Switch statements can be used when any one of the given alternatives are to be matched. In the sense, only when an equality operator is needed. Other relational operators can't be used in switch construct. Switch can be used as a simpler alternative for the if-elseif-else ladder. But the if-elseif-else ladder construct is more efficient that switch

  
Login to rate this answer.
jaspreet grewal

Answered On : Oct 13th, 2011

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

  
Login to rate this answer.

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

Related Open Questions

Ads

Connect

twitter fb Linkedin GPlus RSS

Ads

Interview Question

 Ask Interview Question?

 

Latest Questions

Ads

Interview & Career Tips

Get invaluable Interview and Career Tips delivered directly to your inbox. Get your news alert set up today, Once you confirm your Email subscription, you will be able to download Job Inteview Questions Ebook . Please contact me if you there is any issue with the download.