Menu
Coddy logo textTech

Switch Statement

Part of the Fundamentals section of Coddy's C++ journey — lesson 29 of 74.

The switch statement is like a multi-way if statement. Instead of evaluating a single condition, it checks the value of a variable against multiple cases and executes the code associated with the matching case.

Here's the basic structure of a switch statement:

switch (variable) {
    case value1:
        // Code to execute if variable equals value1
        break;
    case value2:
        // Code to execute if variable equals value2
        break;
    // ... more cases
    default:
        // Code to execute if no case matches
}
  • The switch keyword is followed by the variable you want to test in parentheses.
  • Each case represents a possible value of the variable.
  • The code inside each case is executed if the variable matches that case's value.
  • The break statement is crucial; it exits the switch after a case is executed. Without it, execution would "fall through" to the next case.
  • The default case is optional and is executed if no other case matches.

Here's an example:

int day = 3;
std::string dayName;

switch (day) {
    case 1:
        dayName = "Monday";
        break;
    case 2:
        dayName = "Tuesday";
        break;
    // ... cases for other days
    default:
        dayName = "Invalid day";
}

You can also combine multiple cases into one:

int day = 3;
std::string dayName;

switch (day) {
    case 1:
    case 2:
    case 3:
        dayName = "Start of week";
        break;
    // ... cases for other days
    default:
        dayName = "Invalid day";
}
challenge icon

Challenge

Beginner

Create a program that takes a month number (1 for January, 2 for February, etc.) and prints the season it belongs to. Use a switch statement for the logic.

The seasons and their corresponding months are:

  • Winter: December (12), January (1), February (2)
  • Spring: March (3), April (4), May (5)
  • Summer: June (6), July (7), August (8)
  • Autumn: September (9), October (10), November (11)
  • Invalid month: for other options

Cheat sheet

The switch statement is a multi-way conditional that checks a variable against multiple cases:

switch (variable) {
    case value1:
        // Code to execute if variable equals value1
        break;
    case value2:
        // Code to execute if variable equals value2
        break;
    default:
        // Code to execute if no case matches
}

Key components:

  • case: represents a possible value of the variable
  • break: exits the switch after a case is executed (prevents fall-through)
  • default: optional case executed if no other case matches

You can combine multiple cases:

switch (day) {
    case 1:
    case 2:
    case 3:
        dayName = "Start of week";
        break;
    default:
        dayName = "Invalid day";
}

Try it yourself

#include <iostream>

int main() {
    int month;
    std::cin >> month;
    std::string season = "";
    
    // Write your code below
    
    
    std::cout << season << std::endl;
    return 0;
}
quiz iconTest yourself

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

All lessons in Fundamentals