Jump to content
Larry Ullman's Book Forums

Ch. 2 Pg 48 - Percentage Of Time Using Switch Vs. If/else-If/else?


Recommended Posts

I don't think it's a percentage thing so much as a what-does-the-scenario-call-for thing.

 

Switch statements are generally preferable when you have a bunch of string or number values that a variable can be, and you want to switch up the behavior based on what the current value is.

If statements are generally more for making complex comparisons that aren't simply, "Is this variable equal to this value?"

 

As a side note, an array or object (i.e., hash table) can often be used as a more efficient replacement of a switch statement.

Please Google it for more details.

Link to comment
Share on other sites

 Share

×
×
  • Create New...