On this page11 sections
Lecture 052: Fall-Through in switch
| Day | Extra Topics |
| Lecture | 052 of 057 |
| Estimated video length | 8 min |
| Target | Dart 3.12 |
| Level | Beginner |
In some languages, once a
switchcase matches, the code "falls through" into the next case unless you writebreak. Dart works differently: a case with code stops on its own. This lecture shows how to share one block between several cases, and how to jump on purpose withcontinue.
Contents
Prerequisites · Objectives · Lesson · Code Examples · Common Mistakes · Practice Questions · Answers · Takeaways
Prerequisites
Lecture 019 (switch and case) and Lecture 024 (labels).
Learning Objectives
- Explain what fall-through means
- Know that a Dart case with code never falls through
- Share one block between several cases using empty cases
- Combine cases with
|| - Jump to another case on purpose with
continueand a label
1. What Fall-Through Means
In C or Java, a matching case keeps running into the next case's code until it reaches break.
Forgetting break is a classic bug. In Dart, a case that has code stops automatically:
var x = 1;
switch (x) {
case 1:
print('one');
case 2:
print('two');
}Output
oneOnly one. There's no need for break (it's allowed, but it changes nothing at the end of a
case).
2. Empty Cases Fall Through
A case with no code falls through to the next one. That's how several values share one block:
var day = 'sat';
switch (day) {
case 'sat':
case 'sun':
print('Weekend');
case 'mon':
case 'tue':
case 'wed':
case 'thu':
case 'fri':
print('Weekday');
}Output: Weekend
'sat' has no code, so it falls into 'sun''s block, prints Weekend, and stops.
3. Combining Cases With ||
Dart 3 lets you write the alternatives on one line:
var grade = 'B';
switch (grade) {
case 'A' || 'B':
print('Well done');
case 'C':
print('Passed');
default:
print('Try again');
}Output: Well done
4. Jumping On Purpose With continue
To run one case and then another case's code too, label the target case and use
continue label;:
var level = 2;
switch (level) {
case 1:
print('Level 1: basics unlocked');
continue level2;
level2:
case 2:
print('Level 2: tools unlocked');
continue level3;
level3:
case 3:
print('Level 3: final boss unlocked');
}Output (for level 2)
Level 2: tools unlocked
Level 3: final boss unlockedStarting at level 1 would print all three lines. Every jump is written out, so nothing falls through by accident.
Code Examples
Example 1: Seasons From Month Numbers
void main() {
for (var month in [1, 4, 7, 10, 12]) {
var name = '';
switch (month) {
case 12:
case 1:
case 2:
name = 'Winter';
case 3:
case 4:
case 5:
name = 'Spring';
case 6:
case 7:
case 8:
name = 'Summer';
default:
name = 'Autumn';
}
print('Month $month: $name');
}
}Output
Month 1: Winter
Month 4: Spring
Month 7: Summer
Month 10: Autumn
Month 12: WinterEach group of empty cases falls into one shared assignment. default catches 9, 10 and 11.
Common Beginner Mistakes
1. Expecting C-style fall-through: a case with code does not run into the next case.
If you need that, use continue label;.
2. Code after break
case 1:
print('one');
break;
print('never'); // never runsThere's no error, but the line after break can never run. Delete it.
3. Putting a statement in a case you meant to share: case 'sat': print('Sat!'); case 'sun': ...
now stops after printing Sat!; it no longer shares the weekend block. Keep shared cases empty,
or use case 'sat' || 'sun':.
Practice Questions
- In Dart, does a case with code run into the next case?
- How do you make
'sat'and'sun'share one block? Give two ways. - Is
breakrequired at the end of a case? - Which keyword jumps from one case to a labelled case?
- In the seasons example, what does month
11print?
Practice Answers
- No. It stops automatically.
- Stack empty cases (
case 'sat': case 'sun':), or writecase 'sat' || 'sun':. - No. It's allowed but not needed.
continue, followed by the label name.Month 11: Autumn, fromdefault.
Key Takeaways
- Dart cases with code never fall through, so a missing
breakcan't cause bugs. - Empty cases fall into the next case; use them to share a block.
case a || b:is a shorter way to share a block.continue label;jumps to another case on purpose.