On this page11 sections
  1. Prerequisites
  2. Learning Objectives
  3. 1. What Fall-Through Means
  4. 2. Empty Cases Fall Through
  5. 3. Combining Cases With ||
  6. 4. Jumping On Purpose With continue
  7. Code Examples
  8. Common Beginner Mistakes
  9. Practice Questions
  10. Practice Answers
  11. Key Takeaways

Lecture 052: Fall-Through in switch

DayExtra Topics
Lecture052 of 057
Estimated video length8 min
TargetDart 3.12
LevelBeginner

In some languages, once a switch case matches, the code "falls through" into the next case unless you write break. 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 with continue.

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 continue and 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:

dart
var x = 1;
switch (x) {
  case 1:
    print('one');
  case 2:
    print('two');
}

Output

code
one

Only 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:

dart
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:

dart
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;:

dart
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)

code
Level 2: tools unlocked
Level 3: final boss unlocked

Starting 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

dart
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

code
Month 1: Winter
Month 4: Spring
Month 7: Summer
Month 10: Autumn
Month 12: Winter

Each 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

dart
case 1:
  print('one');
  break;
  print('never');   // never runs

There'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

  1. In Dart, does a case with code run into the next case?
  2. How do you make 'sat' and 'sun' share one block? Give two ways.
  3. Is break required at the end of a case?
  4. Which keyword jumps from one case to a labelled case?
  5. In the seasons example, what does month 11 print?

Practice Answers

  1. No. It stops automatically.
  2. Stack empty cases (case 'sat': case 'sun':), or write case 'sat' || 'sun':.
  3. No. It's allowed but not needed.
  4. continue, followed by the label name.
  5. Month 11: Autumn, from default.

Key Takeaways

  • Dart cases with code never fall through, so a missing break can'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.