0
What is the major difference between elst-if statement and switch statement?
when we have to use else-if statement and switch statement? If we use else-if statement why we can't use switch statement instead of else-if statement . Or if we use switch statement why we can't use else-if statement instead of switch statement.
3 ответов
+ 4
You can. A switch-statement is simply a quicker, easier way to do several else-if statements.
+ 4
All switch statements can be turned into an if statement.
However, NOT all if statements can be turned into a switch.
Switch statements are very optimized, not sure how the compiler deals with them but they have increased performance. This is what they were created for.
*Note* that switch statements can only be used for:
Strings, Enums, chars, or int's. Only these types, and maybe some other special ones can be turned into a switch case.
(int includes short and byte)
+ 3
Switch statement is just a shortcut, so you can use switch instead of having to write multiple else if statements.