Switch Statements to the Rescue

Ernesto Rocha
2 min readJun 18, 2021

What happens if we have tons of powerup? It would be a complete nightmare in an if statement. Since we have a powerup ID for our powerups, there is a more efficient way to identify objects directly.

We can use a switch statement instead of an if statement.

In the switch statement, we can send in the value we want our switch to verify. Sort these values in separate cases and always end it with a break.
And there can also have a default to deal with the value that is not listing in the cases, just like the else if statement.

It can be changed like this:

In this statement, we simply told our Powerup script that if the ID is 0, switch to triple shot, if it is 1, give it a speed boost, and so on.
The switch statement will check the value we have imported and switch to the right one.

Then we should have exactly the same result when we play it.

--

--