Hi folks,
This is only my 3rd question on this forum, but you people are fantastic about answering quickly and with great clarity so thanks ahead of time! My question is about when to use a "switch" statement over an "if else" ladder statement. I think I have a fair grasp of how they work, but it seems to me that the switch statement saves no appreciable amount of coding over an if/else ladder, and the switch statement is limited to use with byte, short, int,
String, char, and enum I think (I just remember those by thinking that you CANNOT use decimals, long, or boolean...all else is cool). Also you can't use statements like if(x > 4 && x < 9), which you CAN use in an if/else ladder.
So....saves no real coding space, isn't simpler like a ternary statement is to an if/else statement, can't be used with as many variable types, and can't be used to make more complex decisions like if(x > 4 && x < 9).
Seems to me that the if/else ladder wins hands down. What am I missing? There must be some situation where it is of great advantage to use a switch statement or I'm guessing they wouldn't have invented it.
Thanks wise coding people!