Menu
Coddy logo textTech

Enums in Switch Statements

Part of the Logic & Flow section of Coddy's C journey — lesson 57 of 63.

One of the most powerful combinations in C programming is using enum values with switch statements. This pairing creates exceptionally readable and maintainable control flow logic that clearly expresses your program's decision-making process.

When you use enum constants as cases in a switch statement, the code becomes self-documenting. Instead of seeing mysterious numbers like case 0: or case 5:, you see meaningful names that immediately convey their purpose:

switch (day) {
    case MONDAY:
    case TUESDAY:
    case WEDNESDAY:
    case THURSDAY:
    case FRIDAY:
        printf("It's a weekday\n");
        break;
    case SATURDAY:
    case SUNDAY:
        printf("It's a weekend\n");
        break;
}

This approach makes your code much easier to understand and modify. Anyone reading this code immediately knows what each case represents, and adding new cases or changing the logic becomes straightforward. The compiler also helps catch errors - if you misspell an enum constant, you'll get a compilation error rather than a silent bug.

challenge icon

Challenge

Easy

Create a C program that uses an enum with a switch statement to build a simple weather advisory system. Your program should:

  1. Define an enum named WeatherCondition with the following constants in this exact order:
    • SUNNY
    • CLOUDY
    • RAINY
    • STORMY
    • SNOWY
  2. Write a function named getWeatherAdvice that:
    • Takes an enum WeatherCondition parameter named weather
    • Returns nothing (void)
    • Uses a switch statement to print weather-specific advice based on the condition:
      • For SUNNY: print Perfect day for outdoor activities!
      • For CLOUDY: print Good day for a walk, no sun protection needed.
      • For RAINY: print Don't forget your umbrella!
      • For STORMY: print Stay indoors and avoid travel.
      • For SNOWY: print Drive carefully and dress warmly.
  3. Write a function named getActivitySuggestion that:
    • Takes an enum WeatherCondition parameter named weather
    • Returns nothing (void)
    • Uses a switch statement to suggest activities based on the weather:
      • For SUNNY: print Suggested activity: Beach or hiking
      • For CLOUDY: print Suggested activity: Photography or gardening
      • For RAINY: print Suggested activity: Reading or indoor games
      • For STORMY: print Suggested activity: Movie marathon
      • For SNOWY: print Suggested activity: Skiing or hot cocoa
  4. In the main function:
    • Declare a variable of type enum WeatherCondition named currentWeather
    • Read an integer from input representing the weather condition (0, 1, 2, 3, or 4)
    • Assign the corresponding enum value to currentWeather based on the input
    • Print the current weather condition in this exact format: Current weather: [numeric_value]
    • Call the getWeatherAdvice function with currentWeather
    • Call the getActivitySuggestion function with currentWeather

This challenge demonstrates the power of combining enums with switch statements to create readable, maintainable code. Instead of using confusing numeric cases in your switch statements, you'll use meaningful enum constants that clearly express the program's logic. The switch statement becomes self-documenting, making it easy to understand what each case represents and modify the behavior as needed.

Cheat sheet

Using enum values with switch statements creates readable and maintainable control flow logic:

switch (day) {
    case MONDAY:
    case TUESDAY:
    case WEDNESDAY:
    case THURSDAY:
    case FRIDAY:
        printf("It's a weekday\n");
        break;
    case SATURDAY:
    case SUNDAY:
        printf("It's a weekend\n");
        break;
}

This approach makes code self-documenting by using meaningful names instead of mysterious numbers like case 0: or case 5:. The compiler helps catch errors by generating compilation errors for misspelled enum constants rather than silent bugs.

Try it yourself

#include <stdio.h>

// TODO: Define the WeatherCondition enum here

// TODO: Implement the getWeatherAdvice function here

// TODO: Implement the getActivitySuggestion function here

int main() {
    // Read input
    int weatherInput;
    scanf("%d", &weatherInput);
    
    // TODO: Declare currentWeather variable and assign enum value based on input
    
    // TODO: Print current weather condition in format "Current weather: [numeric_value]"
    
    // TODO: Call getWeatherAdvice function
    
    // TODO: Call getActivitySuggestion function
    
    return 0;
}
quiz iconTest yourself

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

All lessons in Logic & Flow