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