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

C++ switch Statement

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 <iostream>

int main() {
	int day = 3;
	switch (day) {
		case 1:
			std::cout << "Monday" << std::endl;
			break;
		case 2:
			std::cout << "Tuesday" << std::endl;
			break;
		case 3:
			std::cout << "Wednesday" << std::endl;
			break;
	}
	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 <iostream>

int main() {
	int day = 1;
	switch (day) {
		case 1:
			std::cout << "Monday" << std::endl;
		case 2:
			std::cout << "Falls through to Tuesday" << std::endl;
			break;
		case 3:
			std::cout << "Wednesday" << std::endl;
			break;
	}
	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 <iostream>

int main() {
	int day = 9;
	switch (day) {
		case 1:
			std::cout << "Monday" << std::endl;
			break;
		default:
			std::cout << "Invalid day" << std::endl;
	}
	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 <iostream>

int main() {
	int day = 6;
	switch (day) {
		case 1:
		case 2:
		case 3:
		case 4:
		case 5:
			std::cout << "Weekday" << std::endl;
			break;
		case 6:
		case 7:
			std::cout << "Weekend" << std::endl;
			break;
	}
	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 <iostream>

int main() {
	int category = 1;
	int size = 2;
	switch (category) {
		case 1:
			switch (size) {
				case 1:
					std::cout << "Small shirt" << std::endl;
					break;
				case 2:
					std::cout << "Medium shirt" << std::endl;
					break;
			}
			break;
	}
	return 0;
}

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.