Switch Statements to the Rescue

If you have read my article from yesterday, you have probably observed that I have used switch statements instead of if-else statements on the power-up script.

Actually, it took me some time to get used to switch statements. You know, if you have taken a simple programming course, if-else statements are easy to understand and use. Thus, I was usually avoiding switch statements, as I was already getting the same result with the if-else statements.

Well, it turned out that I was missing an important knowledge.

First of all, I really had no idea that switch statements were more efficient than using if-else statements. Using switch statements also look nicer, and really easy to use. It is recommended to use switch statements rather than if-else statements if we have more than 2 conditions.

Switch statements also checks for a given condition for multiple values, just like If-else statements. Except from the difference that switch statements are only used for exact values rather than ranges, if-else and switch statements can be used for the same purpose.

Here is a statement with if-else statement:

As you can observe, this does not look much tidy. And it would be chaotic if we had tens of cases.

The logic to use switch condition is pretty straightforward:

First we state the name of the condition without the value after switch. After that, we state the case with the value for the condition. After typing in the action for the case, we end the case with break and continue with the other cases. Just like the else statement in the if-else statements, we end the switch statements with default line, stating the action to be helded if the statement results in a value different from the stated cases.

switch (condition)
case x:
Debug.Log("insert the action you want here");
case y:
Debug.Log("action for the y case");

By using switch statements, we change the if-else statement given above as follows:

Looks more tidy, and it is really easier to read. It is better for us to get used to switch statements in the way of more efficient programming.




Unity Developer

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

8 Levels of Using List Comprehension in Python

The concepts of Kubernetes and creating a GKE cluster on Google Cloud Platform (GCP)

Deploy a web server apache httpd on RedHat-like systems — Ansible modules yum, copy, service…

My First Attempt At Building a Full Stack Application

A Beginner’s Dive into GraphQL — Part 4— GraphQL Directives and Mutations

Top DevOps Mistakes to Avoid

Stripe CLI / Stripe API : Create Plan and Product -> Create Customer -> Create Payment method as…


CS 373: Week of 4/5

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
Ali Emre Onur

Ali Emre Onur

Unity Developer

More from Medium

Making Camera Shake using Impulse Listener Extensions

How to Use Post Processing in Unity

Using Signal Emitters with Timeline in Unity 2020

How to Add Emissions to Custom Textures!