Results 1 to 6 of 6

Thread: What is the difference between Switch and If Else?

  1. #1
    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 08:21 AM.

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    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 08:22 AM.

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    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.

  4. #4
    Join Date
    Apr 2008
    Posts
    2,005

    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.

  5. #5
    Join Date
    May 2008
    Posts
    2,297

    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.

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

Similar Threads

  1. Managed switch network speed vs. unmanaged switch network speed
    By LLoLL in forum Networking & Security
    Replies: 6
    Last Post: 22-07-2011, 07:28 PM
  2. Replies: 4
    Last Post: 14-04-2011, 02:06 PM
  3. Difference between hub and switch ?
    By KABIRA in forum Networking & Security
    Replies: 4
    Last Post: 03-02-2009, 11:56 AM
  4. Difference between a switch and a router
    By Volodymyra in forum Networking & Security
    Replies: 2
    Last Post: 09-12-2008, 05:20 PM
  5. Difference between Router Gateway Repeater Hub Bridge Switch ?
    By Kelsey in forum Networking & Security
    Replies: 13
    Last Post: 05-12-2008, 06:21 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,711,666,526.56488 seconds with 17 queries