Lesson 15: switch
When checking one value against many options, an if/else if chain gets long. switch is cleaner: switch (value) checks the value, each case is an option, break ends it, and default runs if no case matched. Note: in C# each case that does something must have a break.
switch is like a switchboard: based on the value, the matching case lights up. break is 'I'm done here', and default is 'if nothing matched'.
- switch (value)
- Checks one value and selects the case that matches it.
- case
- One option in a switch: case 3: runs if the value is 3.
- break
- Ends the current case so it doesn't continue into the next one.
- default
- Runs when no case matched — like the else of a switch.