Monday, January 29, 2018

Java: Switches

Throughout my time programming, there have been many times where I needed to get an input from a user, and depending on what they inputted, I needed to run different code. There are multiple ways of accomplishing this, however some methods are faster than others and much easier to work with.

In most programming, the way to do this would be to use a lot of if and else statements, chained together. The first if statement checks if a certain value is true, and if so, it runs the following code. However, if the value is false, it goes on to the else statement, or else if statement. The else statement runs the next code without question, however the else if statement acts just like the first if statement. This means that you can chain lots of these together to test for different possible inputs, and only the code for the user's input will be run.

As you can see, this option will get long and confusing as more options are added due to the difficulty to read the code. There is so much extra code that is required that the code that is more important is easily lost. Barry Burd, a computer science educator with a PhD, wrote that lots of if statements seems wasteful and "why not create a statement that checks the value of [a variable] just once and then takes an action based on the value it finds?" (Burd 132). Luckily, there is a simple solution to the problem. Several years ago, a new feature was implemented into Java that allowed switches to replace the complex if and else statements. This new method was short, simple and easy to use and understand, plus it was scalab.e This meant that if there was anything that needed changes, it would be easy to change.

As you can see in the second image, each if statement has been replaced by a case statement inside of the switch. The switch defines what variable to check, and the cases define what options the variable can be. Burd explained that "the computer checks the value of the [inputted] variable. When the computer determines that the [inputted] variable's value is 2, the computer checks the case of the switch statement. The value 2 doesn't match the topmost case, so the computer proceeds to the middle of the three cases. The value posted for the middle case (the number 2) matches the value of the [inputted] variable, so the computer executes the statements that come immediately after case 2" (Burd 133). This was still a problem for me, because if a user inputted text such as "strONGly agRee", the code wouldn't recognize it. To solve this, I decided to first make the variable lowercase, then checked for all lowercase answers as there is no way to ignore the case of each answer as I could in the first method.

Other than that, this method is extremely useful and much easier to use! The code is simpler, easier to understand and easier to add on to if future changes are necessary. All of this adds up to make switches a great addition to the programming language, and something that should be taken full advantage of!

Have you ever tried to repeat a task over and over, only to get tired of it before you were even done?

Works Cited:
Burd, Barry. Java for Dummies. 7th ed., John Wiley & Sons, 2017.

No comments:

Post a Comment