Nerd For Tech
Published in

Nerd For Tech

Switch Statements & Enumeration Types

In programming, there is almost always more than one way to do things. Sometimes one method is more performant than another, but usually, it comes down to personal preference and clarity of code.

The difference between chaining if-else statements vs. a switch statement falls mainly in the latter, but in long if-else chains, it is more performant to use a switch in most cases. By long if-else chains, I mean three or more if-else statements. Using an Enum falls almost strictly in the personal preference and clarity of the code part.

Switch

A switch is a selection statement that chooses a single case to execute from a list of candidates based on a match with the defined in the switch. The switch statement is often used as an alternative to an if-else chain if checking a single variable in three or more conditions.

To create a switch, you start with the switch keyword followed by the variable that will be checked in parenthesis; then, you make the cases inside the switch statement. A case is an if statement boiled down to just the keyword case and the condition followed by a colon. Then on the following line, you add the code you want to execute if the condition matches, and after that, on the next line, you use the keyword break to tell the switch a match was found and to stop checking. After adding all your cases, it is required to have a default: to catch any value out of the range of the cases so that the switch doesn’t keep running.

Example Switch

In previous articles, we created a PowerUp script that uses an if-else chain that checks the _powerUpID three times to know which PowerUp to activate on the Player. Let’s make this a switch statement. First, create a switch that checks _powerUpID and create cases for 0, 1, 2, and default. Then have the cases execute the correct code on the player for the PowerUp, TripleShot for 0, SpeedBoost for 1, and Shield for 2.

if-else chain (left) vs. switch (right)

Enumeration Type

An enumeration type (or enum type) is an instance of the type defined by a set of named constants (unchangeable/read-only variables) of the type integer. By default, the enum items’ constant values are type int, starting at 0 and counting up with each item, but the enum items’ constant value can be set manually to any integer.

To create an enum, set the accessibility level of it, use the enum keyword followed by what you want to name, use the same naming convention as a method. Inside of it, you can name your enum items and separate them with a comma, and now the enum is made, but you need a variable of it to hold the setting for the current constant. To do this, you create a variable with the Type as the enum name and then the variable’s name and have it equal the constant of the enum you want to be the default value. If you want to see the drop-down menu in Unity, make sure to make the variable public or serialized.

Example Enum

In the previous article, we used Tooltip to show what values corresponded to which PowerUp activation would pop up in Unity when you hover over the variable name.

Tooltip Code

Now we will change this to an enum to make it even easier to adjust in Unity. First, create an enum named “PowerUpType”, inside it add TripleShot, SpeedBoost, and Shield. Next, create the variable of Type PowerUpType named “_powerUpType”, and have it equal PowerUpType.TripleShot. Make sure the variable is visible in Unity. The only thing left to do now is to have the Switch statement use the Enum variable.

PowerUpType Enum

Change the switch’s variable to _powerUpType, then change case conditions to the enum constant items, 0 to TripleShot, 1 to SpeedBoost, and 2 to Shield.

Switch Using Enum Variable

In Unity, you can now select the different PowerUp Types from the drop-down menu. It is now easy to see which does what, and you can not select a number out of the range of the switch.

Power Up Type Drop Down Menu

--

--

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit https://www.nerdfortech.org/.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store