The switch statement is basically an enhanced version of the "if-else" statement that is more convenient to use when you have code that needs to choose a path from many to follow. Let's have a look at this statement's general syntax:
Ok, not exactly self-explanatory, but it's actually quite simple. The switch statement always begin with the keyword "switch", plus a required parameter that contains the expression (or variable) you wish to evaluate. This expression is then matched against the value following each "case", and if there is a match, it executes the code contained inside that case. If no match is found, it executes the default statement at the end of the switch statement. In the example code on the right, variable "favoritemovie" contains the name of a surfer's favorite movie. If his selection happens to be either "Titanic", "Waterworld", or "Scream 2", a customized message is alerted. All other selections will simply result in a generic, "I'm sure it was great" message.
A switch statement is essentially the same as a series of "if" and "else-if" statements. "So, what's the point of even having a switch statement", you may ask. Well, it's simple to use, logical in syntax, and most importantly, many programmers swear by it. You don't have to adopt it when writing conditional statements; in fact, you won't even notice a difference. But as my old science teacher used to say "Try it, you'll like it!"
The follow uses the switch statement to create a "redirection" script based on the day of the week: