Switch Statements to the rescue!

In the previous article, we went over the process of creating a Modular Power-Up System where we used if-else statements to detect the power-ups and then execute their effects accordingly. Even though using if-else statements to accomplish this logic r…


This content originally appeared on Level Up Coding - Medium and was authored by Dennisse Pagán Dávila

In the previous article, we went over the process of creating a Modular Power-Up System where we used if-else statements to detect the power-ups and then execute their effects accordingly. Even though using if-else statements to accomplish this logic results in a functional code, it is best practice to use a switch statement if you have multiple if-else. It makes your code more concise and clearer to understand. Furthermore, in the event that you have 10+ power-ups, the implementation will be more optimized, and easier to achieve this way.

How and when to use them

A good rule of thumb to remember is to always use Switch Statements if your code requires more than two if-else statements. Switch Statements allows us to check for a variety of different cases, then execute the necessary actions all in one neat block of code.

  • condition: An expression that can be an integer(int), an enumerate, or character type expression.
  • case: Each is followed by a value that is being compared to the expression. Then, rather than using a semicolon(;), a colon(:) is used to determine that the next line of code is an executable action.
  • break: Denotes that you are done and no further actions are needed in that particular case.

You can have any number of cases in a Switch Statement. A case is executed when the value in the condition expression is equal to the compared value in a given case.

How if-else statements can be converted into Switch Statements

Let’s take a look a the Modular Power-Up System code from the previous article.

The code makes use of multiple if-else statements to activate the different power-ups, this can be cleaned up and, optimized by converting it to a switch statement.

  1. You still need to create a global int variable to keep track of the current power-up ID. This will be the expression in the Switch Statement.

2. I used the inspector to set the respective values to my power-up objects, this is how the code will differentiate them later.

3. Previously, we had a set of int variables assigned to each power-up, this is not necessary for the Switch Statement. With the expression alone, you can start the comparison directly. The cases can have any int valued assigned to them since the expression is of type int as well. I decided to go with ascending values, but these numbers could have had any order as long as they remain the same type as the expression. Your power-up behavior code should go under the case in the switch statement — it’s the equivalent of saying “in case this happens, do this”

Note: The numbers assigmend to the power-up objects in the inspector must match the ones in the code.
Note:You may notice that you get an error or the foreboding curly red line under your code when writing a Switch statement, this is because the compiler is waiting for you to add the break.

Now, you have a fully functional and optimized Modular Power-Up System! In my next article, we’ll take a look at building UI elements in Unity!


Switch Statements to the rescue! was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.


This content originally appeared on Level Up Coding - Medium and was authored by Dennisse Pagán Dávila


Print Share Comment Cite Upload Translate Updates
APA

Dennisse Pagán Dávila | Sciencx (2021-04-26T15:49:08+00:00) Switch Statements to the rescue!. Retrieved from https://www.scien.cx/2021/04/26/switch-statements-to-the-rescue/

MLA
" » Switch Statements to the rescue!." Dennisse Pagán Dávila | Sciencx - Monday April 26, 2021, https://www.scien.cx/2021/04/26/switch-statements-to-the-rescue/
HARVARD
Dennisse Pagán Dávila | Sciencx Monday April 26, 2021 » Switch Statements to the rescue!., viewed ,<https://www.scien.cx/2021/04/26/switch-statements-to-the-rescue/>
VANCOUVER
Dennisse Pagán Dávila | Sciencx - » Switch Statements to the rescue!. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/04/26/switch-statements-to-the-rescue/
CHICAGO
" » Switch Statements to the rescue!." Dennisse Pagán Dávila | Sciencx - Accessed . https://www.scien.cx/2021/04/26/switch-statements-to-the-rescue/
IEEE
" » Switch Statements to the rescue!." Dennisse Pagán Dávila | Sciencx [Online]. Available: https://www.scien.cx/2021/04/26/switch-statements-to-the-rescue/. [Accessed: ]
rf:citation
» Switch Statements to the rescue! | Dennisse Pagán Dávila | Sciencx | https://www.scien.cx/2021/04/26/switch-statements-to-the-rescue/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.