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.

10th Jul 2017, 2:27 AM
lzy gyz
lzy gyz - avatar
3 Respostas
+ 4
You can. A switch-statement is simply a quicker, easier way to do several else-if statements.
10th Jul 2017, 2:52 AM
Keto Z
Keto Z - avatar
+ 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)
10th Jul 2017, 3:16 AM
Rrestoring faith
Rrestoring faith - avatar
+ 3
Switch statement is just a shortcut, so you can use switch instead of having to write multiple else if statements.
10th Jul 2017, 2:52 AM
onruul
onruul - avatar