posted 19 years ago
A few notes about default, and break:
1. default can occur in lieu of any "case label" in the switch/case statements. It doesn't have to be last.
2. In a switch statement, the value of the switch expression is calculated, and control goes directly to the corresponding label without executing any other code. Code associated with that label begins execution.
3. Execution of code continues until a break is encountered or end of the switch block is encountered.
So if there is code associated with a case label and that code doesn't end with a break, the system keeps executing the next case's code (or the default code) until it sees a break, or the end of the block, whichever comes first.
So it may execute one case's code, then the default and whatever is after that until it sees a break.
In the first example, k was 10 so control immediately went to the label associated with 10 (case 10). All the rest of the code in the block was executed because there was no break at all. (It executes regardless of other case labels).
In the second example, control went to label 10, and executed its associated code. There was no break, so execution continued with code that was normally associated with 20, and the default.
[ October 21, 2004: Message edited by: Louie van Bommel ]