Jump to content


Photo

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







  • Please log in to reply
5 replies to this topic

#1 mytheensha






Posted 05 April 2006 - 05:05 PM

- PLEASE SHARE

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


#2 Abhishek_Debugger

Abhishek_Debugger

    Newbie

  • Members
  • Pip
  • 3 posts

Posted 06 April 2006 - 12:06 PM

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

#3 archanakarpoora

archanakarpoora

    Newbie

  • Members
  • Pip
  • 2 posts

Posted 07 April 2006 - 01:05 AM

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-else In switch the multiple stmt need not be enclosed with in a Pair of braces eg; main() { int i=12 switch(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 follows Main() { 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(…..); } }

#4 Sushil Sharma

Sushil Sharma

    Newbie

  • Members
  • Pip
  • 3 posts

Posted 07 April 2006 - 05:12 PM

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.

#5 vasu_harish15

vasu_harish15

    Newbie

  • Members
  • Pip
  • 6 posts

Posted 23 April 2006 - 07:53 AM

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

#6 SunnyPalSingh

SunnyPalSingh

    Member

  • Members
  • PipPip
  • 24 posts

Posted 25 April 2006 - 02:23 PM

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