Let's simulate a tabletop game scenario with a switch statement and fall-through case, where a dice roll determines the outcome of a specific action:
- Create an int variable, named diceRoll, and assign it a value of 7.
- Declare a switch statement with diceRoll as the match expression.
- Add three cases for possible dice rolls: 7, 15, and 20.
- Case 15 and 20 should have their own debug logs and break statements, while case 7 should fall through to case 15.
- Save the file and run it in Unity:
If you want to see the fall-through case in action, try adding a debug log to case 7, but without the break keyword.
With diceRoll set to 7, the switch will match with the first case, which will fall through and execute case 15 because it lacks a code block and a break statement. If you change diceRoll to 15 or 20, the console will show their respective messages, and any other value will fire off the default case at the end of the...