Menu
Coddy logo textTech

Enums in 'switch' Statements

Part of the Logic & Flow section of Coddy's Dart journey — lesson 61 of 65.

One of the most powerful applications of enums is using them with switch statements. When you combine enums with switch statements, you create code that is both safer and more maintainable.

Here's how you can use an enum in a switch statement:

enum Status {
  pending,
  running,
  completed
}

Status currentStatus = Status.pending;

switch (currentStatus) {
  case Status.pending:
    print('Task is waiting to start');
    break;
  case Status.running:
    print('Task is currently in progress');
    break;
  case Status.completed:
    print('Task has been finished');
    break;
}

The beauty of using enums with switch statements is that Dart can verify you've handled all possible enum values. If you forget to include a case for one of the enum values, Dart will warn you, helping prevent bugs in your code.

This approach is much safer than using strings or numbers in switch statements, where typos could cause unexpected behavior. With enums, you get compile-time checking that ensures your code handles every possible scenario.

challenge icon

Challenge

Easy

Create a program that manages a game character's state using enums and switch statements. Your task is to:

  1. Define an enum called CharacterState with the following values: idle, walking, running, jumping, and attacking
  2. Create a variable called currentState of type CharacterState and assign it the value CharacterState.idle
  3. Create a function called getStateMessage that takes a CharacterState parameter and returns a string message based on the state using a switch statement:
    • For CharacterState.idle: return "Character is standing still"
    • For CharacterState.walking: return "Character is moving slowly"
    • For CharacterState.running: return "Character is moving quickly"
    • For CharacterState.jumping: return "Character is in the air"
    • For CharacterState.attacking: return "Character is fighting"
  4. Print the initial state message using the function with the exact format: Initial state: Character is standing still
  5. Change the currentState to CharacterState.walking
  6. Print the updated state message with the exact format: Updated state: Character is moving slowly
  7. Change the currentState to CharacterState.attacking
  8. Print the final state message with the exact format: Final state: Character is fighting

Your program should demonstrate the proper use of enums with switch statements, showing how this combination provides type-safe state management. The switch statement must handle all enum values and return the appropriate message for each character state. Make sure to follow Dart naming conventions and use the exact string messages specified above.

Cheat sheet

Enums work well with switch statements for type-safe code:

enum Status {
  pending,
  running,
  completed
}

Status currentStatus = Status.pending;

switch (currentStatus) {
  case Status.pending:
    print('Task is waiting to start');
    break;
  case Status.running:
    print('Task is currently in progress');
    break;
  case Status.completed:
    print('Task has been finished');
    break;
}

Dart verifies that all enum values are handled in the switch statement, preventing bugs and providing compile-time checking.

Try it yourself

import 'dart:io';

// TODO: Define your CharacterState enum here

// TODO: Create your getStateMessage function here

void main() {
  // TODO: Write your code below
  
  // Create currentState variable and assign it CharacterState.idle
  
  // Print initial state message
  
  // Change currentState to CharacterState.walking
  
  // Print updated state message
  
  // Change currentState to CharacterState.attacking
  
  // Print final state message
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Logic & Flow