An Overview on Switch Statements

Sean Kaleomaikalani Ferreira
2 min readNov 5, 2023

--

This is the base of a Switch-Case

So you are using too many if-statements. Maybe your professor is getting tired of reading so many else-if-else nonsense or maybe you are sick of it yourself! Well, let me be your guide to switch statements!

Switch statements are a variant of if statements with a different focus. While if statements have the flexibility in arguments and parameters that can change from if statement to else if statement, switch statements revolve around one variable. Switch looks at one variable and each case is a possible result.

break; ends the reading of the current code box (leaving the switch statement)

When I learn new code techniques, I usually try to translate it into a sentence structure to make it easier to understand. The switch-case translation is thus;

Looking at X variable, in the case it is Y, then Z.

In the image above, one possible sentence is “Looking at the _color variable, in the case it is 0, then _paint.color should be red.” Hopefully this helps you better understand where a switch statement could look better than an if statement.

--

--