← Back to C++ Course | Chapter 4: Control Flow | Lesson 8 of 13

C++ switch Statement

A switch statement picks one action from a menu based on a value, like pressing a button on a vending machine. break stops it from running into the next choice.
Syntax
cpp
switch (variable) {
  case value1:
    // code
    break;
  case value2:
    // code
    break;
  default:
    // code if no case matches
}

Basic switch Statement

A switch statement compares one variable's value against several possible constant cases and jumps directly to the matching one, which reads more cleanly than a long else-if ladder when you're checking a single value against many discrete options like a menu choice or a day number.

Example: Basic switch Statement

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	// Declare day and set it to 3
	int day = 3;
	// Choose a branch based on the value of day
	switch (day) {
		case 1:
			std::cout << "Monday" << std::endl;
			// Leave the switch/loop
			break;
		case 2:
			std::cout << "Tuesday" << std::endl;
			// Leave the switch/loop
			break;
		case 3:
			std::cout << "Wednesday" << std::endl;
			break;
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}

The break Statement

break exits the switch block immediately once a matching case's code has run; omitting it causes execution to 'fall through' into the next case's code as well, which is occasionally intentional but is a very common source of bugs when forgotten.

Always add break unless fall-through is a deliberate part of your design.

Example: The break Statement

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	// Declare day and set it to 1
	int day = 1;
	// Choose a branch based on the value of day
	switch (day) {
		case 1:
			std::cout << "Monday" << std::endl;
		case 2:
			std::cout << "Falls through to Tuesday" << std::endl;
			// Leave the switch/loop
			break;
		case 3:
			std::cout << "Wednesday" << std::endl;
			// Leave the switch/loop
			break;
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}

Using the default Case

The default case catches any value that didn't match one of the explicit case labels, functioning as the switch statement's equivalent of a final else in an if-else ladder.

Including a default case is good practice even when you don't expect unmatched values, since it makes unexpected input visible instead of silently ignored.

Example: Using the default Case

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	// Declare day and set it to 9
	int day = 9;
	// Choose a branch based on the value of day
	switch (day) {
		case 1:
			std::cout << "Monday" << std::endl;
			// Leave the switch/loop
			break;
		// default runs when no case matches
		default:
			std::cout << "Invalid day" << std::endl;
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}

Grouping switch Cases

Stacking multiple case labels directly above one shared block, like case 1: case 2: case 3: followed by one block of code, lets several distinct values trigger identical behavior without duplicating that code for each one.

This relies on the same fall-through behavior that break normally prevents, used here deliberately.

Example: Grouping switch Cases

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	// Declare day and set it to 6
	int day = 6;
	// Choose a branch based on the value of day
	switch (day) {
		case 1:
		case 2:
		case 3:
		case 4:
		case 5:
			std::cout << "Weekday" << std::endl;
			// Leave the switch/loop
			break;
		case 6:
		case 7:
			std::cout << "Weekend" << std::endl;
			// Leave the switch/loop
			break;
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}

Nested switch Statements

Placing a switch statement inside another switch's case is valid for handling layered categorical decisions, such as first branching on a product category and then on a specific product within it.

Nested switches can get hard to follow quickly, so it's worth considering whether separate functions would keep the logic clearer.

Example: Nested switch Statements

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	// Declare category and set it to 1
	int category = 1;
	// Declare size and set it to 2
	int size = 2;
	// Choose a branch based on the value of category
	switch (category) {
		case 1:
			switch (size) {
				case 1:
					std::cout << "Small shirt" << std::endl;
					// Leave the switch/loop
					break;
				case 2:
					std::cout << "Medium shirt" << std::endl;
					// Leave the switch/loop
					break;
			}
			break;
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Forgetting break, so execution falls through into the next case.
  2. Using a non-constant or string in a case, when case labels must be integral constants.
  3. Declaring a variable inside a case without braces, which can cause a 'jump to case label' error.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.