Posted April 5, 2006 · Report post what are the differences b/w switch case and if-else statment,other than syntax Share this post Link to post Share on other sites
abhishekdtc 0 Report post Posted April 6, 2006 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 this post Link to post Share on other sites
archanakarpoorasundaram 0 Report post Posted April 6, 2006 what are the differences b/w switch case and if-else statment,other than syntaxThere 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 this post Link to post Share on other sites
kanthwalkanthwal 0 Report post Posted April 7, 2006 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 this post Link to post Share on other sites
vasu_harish15 0 Report post Posted April 23, 2006 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 this post Link to post Share on other sites
SunnyPalSingh 0 Report post Posted April 25, 2006 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 understandWhy 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. Share this post Link to post Share on other sites