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.

  
Showing Answers 1 - 66 of 66 Answers

Hirma

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

Nagesh Kumar

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

  Was this answer useful?  Yes

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.

  Was this answer useful?  Yes

kiran kumar

  • 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

  Was this answer useful?  Yes

Syam Prasad N

  • Jul 13th, 2006
 

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

  Was this answer useful?  Yes

deep275

  • Aug 8th, 2006
 

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.

datta.s.k

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

  Was this answer useful?  Yes

BHARATESH

  • 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

k4kishore

  • May 1st, 2007
 

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

  Was this answer useful?  Yes

Anu

  • Jun 8th, 2007
 

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

  Was this answer useful?  Yes

Ajay

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

vbalaji_ece

  • Jun 11th, 2007
 

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'

  Was this answer useful?  Yes

Anil Patel

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

  Was this answer useful?  Yes

M.Sandeep Bharadwaj

  • 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

  Was this answer useful?  Yes

aditya

  • Jul 26th, 2007
 

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

  Was this answer useful?  Yes

chandraprabha

  • 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 given letter is vowel or consonant.

  Was this answer useful?  Yes

Amit

  • Aug 6th, 2007
 

[1] More Systematic Code - Great readability

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

  Was this answer useful?  Yes

AKHILESH SINGH

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

  Was this answer useful?  Yes

shailendra singh

  • Aug 27th, 2007
 

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

hari krishna

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

  Was this answer useful?  Yes

ajay

  • Sep 6th, 2007
 

Switch statement take a value as argument and go to a particular case statement?and 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.

  Was this answer useful?  Yes

dinesh_27

  • Sep 19th, 2008
 

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

  Was this answer useful?  Yes

crimola

  • Jan 8th, 2009
 

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.

  Was this answer useful?  Yes

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.

  Was this answer useful?  Yes

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

  Was this answer useful?  Yes

jaspreet grewal

  • Oct 13th, 2011
 

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

  Was this answer useful?  Yes

Give your answer:

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

 

Related Answered Questions

 

Related Open Questions