One of the most valuable functions of if-else statements is that they can be nested inside each other, creating complex logic routes through your code. In programming, we call them decision trees. Just like a real hallway, there can be doors behind other doors, creating a labyrinth of possibilities:
Let's break down the preceding example:
- First, an if statement checks whether we have weaponEquipped. At this point, the code only cares whether it's true, not what type of weapon it is.
- The second if statement checks the weaponType and prints out the associated debug log.
- If the first if statement evaluates to false, the code would jump to the else statement and its debug log. If the second if statement evaluates to false, nothing is printed because there is no else statement.
The responsibility of handling logic outcomes is 100% on the programmer. It's up to you to determine the possible branches or outcomes your code can take.
What you've learned...