Switch statements
When writing a program, if statements are a powerful tool for branching code, but sometimes you need to make a lot of individual checks on a single variable, and filling up 100+ lines of code with “if else” statements is at best unappealing.
This is where switch statements come in. Taking a single variable and comparing it against a case, here typeOfPowerUp is an integer value with an intended range of 0–2. When the variable and case compare true, it executes the code like a typical if statement.
But what if I accidentally assign a value greater than 2 to typeOfPowerUp? As the code is currently, it will simply pass all the cases and not execute any of them, failing gracefully but silently.
Using the default keyword I can have the code react accordingly, here I log that there has been a invalid value assigned. But the original purpose of default is to execute the default behavior, while the different cases handle special cases.