All programming languages have the ability to make decisions and handle logic within various mechanisms. One of these is IF statements. If statements are great, but sometimes can become cumbersome. Imagine the following example…
[code lang=”js”]
// Example IF/ELSE/IF statement
if (someNumber == “1”){
dosomething1();
} else if (someNumber == “2”){
dosomething2();
} else if (someNumber == “3”){
dosomething3();
} else if (someNumber == “4”){
dosomething4();
} else if (someNumber == “5”){
dosomething5();
}
[/code]
The above code is not bad, but rather simple. Imaging more complex logic and even having the need to nest IF statements. It would not take long to get a block of code that is cumbersome.
Note: concerning nesting IF statements…there is nothing wrong with nesting, but it can get to be a pain rather quickly. My ‘rule of thumb’ is to not go more than three levels deep. If I have to do that, then I rethink my program structure and logic. I just find that I tend to get lost if I go beyond that, but there is nothing stopping you from going deeper.
Another logic statement we can use is the ‘Switch’ statement. If I end up having a large block of IFs, such as the aforementioned code, I tend to use a Switch statement for that code block. Let’s see how it works…
[code language=”js”]
// Switch statement example
switch (someNumber) {
case "1":
dosomething1();
break;
case "2":
dosomething2();
break;
case "3":
dosomething3();
break;
case "4":
dosomething4();
break;
case "5":
dosomething5();
break;
}
[/code]
This code is a duplicate of the IF code previously mentioned. So how does it work? The switch statement gets passed an argument parameter for testing. In our example, this is represented by ‘ someNumber’. We can pass a hard coded value or a variable to the switch statement. Within the curly braces, we have several test cases. In our example, we are testing to see whether the value of someNumber is 1, 2, 3, 4 or 5. Let’s imagine our value is “2”. So the first thing the switch statement does is test to see if the value is “1”. It is not, so it skips over the dosomething1() and break statements. It then tests if the value is “2”. In our example, that returns true. So, the code runs the dosomething2() function and then the break statement. What does that break statement do? if immediately stops the switch statement from running. There is no need to go further as we have found a case that returns true.
Switch statements can be a quick and handy way to run through several test cases rather than a lot of IF statements.
One side note…it is a good practice to include a default statement within the switch. This is code that runs if no test cases return true. In other words, a ‘catch all’. Here is the syntax…
[code lang=”js”]
// Switch statement with default example
switch (someNumber) {
case "1":
dosomething1();
break;
case "2":
dosomething2();
break;
case "3":
dosomething3();
break;
case "4":
dosomething4();
break;
case "5":
dosomething5();
break;
default:
runSomething();
break;
}
[/code]
Happy Coding!
Clay Hess