Tip of the Day: Switch

No not Nintendo Switch, but actually the Switch statement in C# language.

The Switch statement basically streamlines the conditions, and it is extremely useful when you to compare a variable against different conditions.

Normally you would use If with the combination of else if to state conditions of the same variable.

If and else if

Now imagine having 10 or 50 conditionals that you want to put into your script, that would be very unintuitive and inefficient.

A better way to do so is using the Switch Statement. The syntax of the Switch is a little different, you have to mention a “case”, write your code for that case, and then “break” the case, and start a new one.

Switch statement

As you can see, this method is way more efficient than using too many else ifs. Note that the “case default” acts as “else”; if none of the cases work, then use the default case.

How would you use the Switch Statement?

--

--

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
Mohamed Hijazi

Mohamed Hijazi

A knowledge seeking biologist who is following his passion into a full time career in Unity / Game Development. https://www.linkedin.com/in/mohamed-hijazi/