Switch Statements

If you have ever stacked multiple if statements on to each other then you know that it gets messy quick. It turns into a long list of words and numbers that takes a while to understand if you don’t already know what's happening. Luckily there is a way to avoid this happening.

Switch statements are similar to if statements but they use cases. The switch statement will take a condition and run the case that matches the condition. In the picture above the switch statement is looking for a number. If that number is 1 it will run case 1. Long switch statements are much easier to read than long if statement chains. In general if you are going to use more than 2 if statements, just use a switch statement instead.

--

--

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