Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read

Sponsored Links



What is the difference between Switch and If Else?

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 23-01-2010
Rum Rum is offline
Member
 
Join Date: Dec 2009
Posts: 17
What is the difference between Switch and If Else?
  

I am beginner programmer in this programming world. These days , i am learning the programming language like Java, C++, C sharp, PHP and DBMS. Right now, i am working( means programming) on the different loops in Java. This looping or branching is useful make program better. Actually, i want to know what is difference between the Switch and If else loop?? What are their advantages and disadvantages?


Last edited by Rum : 23-01-2010 at 07:21 AM.
Reply With Quote
  #2  
Old 23-01-2010
Member
 
Join Date: Apr 2008
Posts: 1,946
Re: What is the difference between Switch and If Else?

Actually it is very difficult to say who is powerful and what is difference between them. I would consider the situation where there would be if with many else if clauses vs a switch with many cases. And if thinking about how such control constructs would likely be implemented, it would seem that both the loops would not be superior to one another in terms of execution speed of the generated code.My suggestion would simply to use loop with which you are comfortable.

Last edited by Praetor : 23-01-2010 at 07:22 AM.
Reply With Quote
  #3  
Old 23-01-2010
Member
 
Join Date: May 2008
Posts: 2,004
Re: What is the difference between Switch and If Else?

According to test done by me, i would say that the switch statement/loop is more fast to execute than the if-else-if ladder/loop. This is because of the compilers ability to optimize the switch statement/loop. While in the case of the if-else-if ladder/loop, the code must process each if statement in the order determined by the programmer, where else each case within a switch statement don't rely on earlier cases, the compiler will be able to re-order the testing in such a way as to provide the fastest execution.
Reply With Quote
  #4  
Old 23-01-2010
Member
 
Join Date: Apr 2008
Posts: 1,999
Re: What is the difference between Switch and If Else?

Good question asked. I think that the any present compiler logic would definitely will be able to handle the statements fairly/easily, this what experts says and i also think the same. I would say that code readability is what we basically need to put up now-a-days. In college/school, they used to teach us If-Else and Switch statements for making sure we do as little checks as needed and that the compiler would help us with the rest. Use any of the loop which makes code readable and understandable.
Reply With Quote
  #5  
Old 23-01-2010
Member
 
Join Date: May 2008
Posts: 2,289
Re: What is the difference between Switch and If Else?

I think most of the people/programmer knows that switch works like if-else blocks and the only difference between them is in many cases can be used in switch and it is simpler. But this is totally wrong thinking. I would say that the only difference is the number of the total conditional operations and the switch is faster than if-else block, and number of conditions it is very important for the if-else block but not very important for the switch.
Reply With Quote
  #6  
Old 23-01-2010
Member
 
Join Date: Nov 2005
Posts: 1,323
Re: What is the difference between Switch and If Else?

If-Else statement: When you want to run one group of code and if the condition is true then an section of code would execute and if the condition is false then another section would execute.
Switch statement: Switch has a very simple but good algorithm. In switch, the condition of a variables or expression is be tested against a numbers of discrete values or cases, unless you can find the matching expression. The code within the matched case is then executed.
The only difference between both them is switch is more simpler and efficient to use rather then if-else statement.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "What is the difference between Switch and If Else?"
Thread Thread Starter Forum Replies Last Post
Managed switch network speed vs. unmanaged switch network speed LLoLL Networking & Security 6 22-07-2011 07:28 PM
Toshiba a665 s6050 eco wireless switch, Led switch and play/pause are not working Emilian Portable Devices 4 14-04-2011 02:06 PM
Difference between hub and switch ? KABIRA Networking & Security 4 03-02-2009 10:56 AM
Difference between a switch and a router Volodymyra Networking & Security 2 09-12-2008 04:20 PM
Difference between Router Gateway Repeater Hub Bridge Switch ? Kelsey Networking & Security 13 05-12-2008 05:21 PM


All times are GMT +5.5. The time now is 01:33 PM.