Dart Switch

Elegant way to handle multiple conditions

🔀 What is Switch Statement?

Switch statements provide a clean way to execute different code blocks based on the value of a variable. It's more readable than multiple if-else statements when checking one variable against many values.


// Simple switch example
void main() {
  String day = 'Monday';
  switch (day) {
    case 'Monday':
      print('Start of work week');
      break;
    case 'Friday':
      print('TGIF!');
      break;
    default:
      print('Regular day');
  }
}
                                    

Switch Statement Features

📋

Case Labels

Define different values to match

case 'value1':
  // code here
  break;
🛑

Break Statement

Prevents fall-through to next case

case 'A':
  print('Excellent');
  break;
🎯

Default Case

Handles unmatched values

default:
  print('No match found');
  break;
⬇️

Fall-through

Execute multiple cases together

case 'A':
case 'B':
  print('Good grade');
  break;

🔹 Basic Switch Statement

Compare a variable against multiple values:

void main() {
  int dayNumber = 3;
  
  switch (dayNumber) {
    case 1:
      print('Monday');
      break;
    case 2:
      print('Tuesday');
      break;
    case 3:
      print('Wednesday');
      break;
    case 4:
      print('Thursday');
      break;
    case 5:
      print('Friday');
      break;
    default:
      print('Weekend');
      break;
  }
}

Output:

Wednesday

🔹 Switch with Strings

Switch statements work with strings too:

void main() {
  String grade = 'B';
  
  switch (grade) {
    case 'A':
      print('Excellent! 90-100%');
      break;
    case 'B':
      print('Good! 80-89%');
      break;
    case 'C':
      print('Average! 70-79%');
      break;
    case 'D':
      print('Below Average! 60-69%');
      break;
    case 'F':
      print('Failed! Below 60%');
      break;
    default:
      print('Invalid grade');
      break;
  }
}

Output:

Good! 80-89%

🔹 Fall-through Cases

Multiple cases can share the same code block:

void main() {
  String month = 'December';
  
  switch (month) {
    case 'December':
    case 'January':
    case 'February':
      print('Winter season');
      break;
    case 'March':
    case 'April':
    case 'May':
      print('Spring season');
      break;
    case 'June':
    case 'July':
    case 'August':
      print('Summer season');
      break;
    case 'September':
    case 'October':
    case 'November':
      print('Autumn season');
      break;
    default:
      print('Invalid month');
      break;
  }
}

Output:

Winter season

🔹 Switch vs If-Else

Compare switch statement with equivalent if-else:

🔸 Using If-Else (longer)

void checkTrafficLight(String color) {
  if (color == 'red') {
    print('Stop');
  } else if (color == 'yellow') {
    print('Caution');
  } else if (color == 'green') {
    print('Go');
  } else {
    print('Invalid color');
  }
}

🔸 Using Switch (cleaner)

void checkTrafficLight(String color) {
  switch (color) {
    case 'red':
      print('Stop');
      break;
    case 'yellow':
      print('Caution');
      break;
    case 'green':
      print('Go');
      break;
    default:
      print('Invalid color');
      break;
  }
}

🔹 Switch Best Practices

Follow these guidelines for effective switch statements:

Good Practices:

  • Always use break: Prevents accidental fall-through
  • Include default case: Handle unexpected values
  • Use for exact matches: Switch works with specific values
  • Keep cases simple: Complex logic should be in functions
// Good practice example
void processUserInput(String command) {
  switch (command.toLowerCase()) {
    case 'start':
      startApplication();
      break;
    case 'stop':
      stopApplication();
      break;
    case 'pause':
      pauseApplication();
      break;
    default:
      print('Unknown command: $command');
      showHelp();
      break;
  }
}

🧠 Test Your Knowledge

What happens if you forget to include 'break' in a switch case?