# What are the differences b/w switch case and if-else statment,other than syntax

what are the differences b/w switch case and if-else statment,other than syntax

##### Share on other sites

what are the differences b/w switch case and if-else statment,other than syntax

see there is not much diff.. But one mail that u can only use integer variable or character variable in switch but in if statement u can use all data dypes..

##### Share on other sites

what are the differences b/w switch case and if-else statment,other than syntax

There is no much more difference but i can say some advantage of if-else over switch and vice versa.*Advantage of switch over if-elseIn switch the multiple stmt need not be enclosed with in a Pair of braceseg;main(){ int i=12switch(i){ case1: Printf(“this is 1”); /* no need of braces for multiple stmt.*/ Print f(“this is first stage”); Printf(“I am in case 1”); Prinntf(“……..”); Break;Case5: Printf(“this is 5”); /* no need of braces for multiple stmt.*/Print f(“this is sec stage”); Printf(“I am in case 5”); Prinntf(“……..”); Break; case12: Printf(“this is 12”); Print f(“this is third stage”); /* no need of braces for multiple stmt.*/ Printf(“I am in case 12”); Printf(“……..”); Break;}} The same can be done in if-else as followsMain(){ int i=12;If(i= =1){ /* need of braces for multiple stmt*/ Printf(“this is 1”); Print f(“this is first stage”); Printf(“I am in case 1”); Printf(“……..”);}Else{ if(i= =5) { Printf(“this is 5”); Print f(“this is sec stage”); Printf(“I am in case 5”); Printf(“……..”); } Else { Printf(“this is 12”); Print f(“this is third stage”); Printf(“I am in case 12”); Printf(“……..”); } /* If we miss close braces it will cause error this is main disadvantage } of if-else */ } *Disadvantage of switch:1.In case we can not give condition(<,><=,etc)Eg; It wont accept stmt Case a<20:2.In “case” datatype float is not allowed.Eg Main(){ int 1=1; char ch=’z’; float a=2.2;switch(i) { case’z’: printf(…..); case ‘1’: printf(…..); case 2.2: /* as it is float this is not allowed*/ printf(…..); }}

##### Share on other sites

There is no difference between switch and if else. It depends upon the programmer to use switch or if else. The compiler might be treating the switch as if else (internally). But if we are using switch we get a clean program and others can easily read the program.

##### Share on other sites

hello the switch statement executes faster than the if else statement because the switch statement check the condition first and jumps to the suitable case statement at the casewheras in the if else it will check every conditions so that the execution time will be more and the code becomes lengthy and become very difficult to understand

##### Share on other sites

the switch statement executes faster than the if else statement because the switch statement check the condition first and jumps to the suitable case statement at the casewheras in the if else it will check every conditions so that the execution time will be more and the code becomes lengthy and become very difficult to understand

Why do you think compiler will not do that for you. Compiler might be internally optimising things for you. There may be a possibilty(when cases are sparsely distributed) that switch may be internally converted to if-else. So, we shouldn't be worried about such things.

## Create an account or sign in to comment

You need to be a member in order to leave a comment

## Create an account

Sign up for a new account in our community. It's easy!

Register a new account