‘Switch’ to a New Process

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

More To Explore

computer, laptop, work place-2982270.jpg
Code

Unlocking Wireless Communication: A Dive into the Bluetooth API

Wireless communication has become an integral part of our daily lives, and Bluetooth technology is at the forefront of this revolution, enabling devices to exchange data over short distances and creating a world more interconnected than ever before. At the heart of this technology lies the Bluetooth Application Programming Interface (API), a powerful tool for developers looking to harness the capabilities of Bluetooth in their applications. In this blog post, we’ll explore what the Bluetooth API is, how it works, and the possibilities it opens up for innovation in wireless communication.

lighthouse, beacon, atlantic-8578318.jpg
Code

Understanding the Beacon API: Simplifying Asynchronous Data Transfers

In today’s data-driven world, web applications often need to send data back to the server. Traditionally, this has been done using AJAX requests or similar methods. However, these techniques can come with a cost, especially when dealing with data that needs to be sent during the unload phase of a document, such as tracking and diagnostic data. This is where the Beacon API shines by allowing developers to send data to a server more reliably and efficiently.

Share This Post

small_c_popup.png

Need help?

Let's have a chat...


Login

Jump Back In!

Here at Webolution Designs, we love to learn. This includes sharing things we have learned with you. 

Register

Begin Your Learning Journey Today!

Come back inside to continue your learning journey.