← Back to C Course | Chapter 4: Control Flow | Lesson 6 of 11

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
c
switch (variable) {
  case value1:
    // code
    break;
  case value2:
    // code
    break;
  default:
    // code if no case matches
}

What is a switch Statement?

A switch statement evaluates one expression once and compares it against a list of case labels, jumping directly to the matching case -- a cleaner alternative to a long else-if ladder when you're comparing one variable against many fixed values.

Example: What is a switch Statement?

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// 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 3:
			printf("Wednesday");
			// Leave the switch/loop
			break;
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}

Day of the Week

Mapping the integers 1 through 7 to day names is a classic switch use case: each case label handles one specific number, making the mapping far more readable than an equivalent chain of if-else comparisons.

Example: Day of the Week

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare day and set it to 2
	int day = 2;
	// Choose a branch based on the value of day
	switch (day) {
		case 1: printf("Monday"); break;
		case 2: printf("Tuesday"); break;
		case 3: printf("Wednesday"); break;
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}

Grade Switch

switch can match character constants too, so a grade switch might map the case A to print "Excellent", letting you branch on single-character input just as easily as on integers.

Example: Grade Switch

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare grade and set it to 'A'
	char grade = 'A';
	// Choose a branch based on the value of grade
	switch (grade) {
		case 'A':
			printf("Excellent");
			// Leave the switch/loop
			break;
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}

The break Keyword

Without an explicit break at the end of a case block, execution "falls through" and continues running the code in the next case as well -- a frequent source of bugs for anyone coming from a language where fall-through isn't the default.

Example: The break Keyword

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare x and set it to 1
	int x = 1;
	// Choose a branch based on the value of x
	switch (x) {
		case 1:
			printf("One ");
		case 2:
			printf("Falls through to Two");
			// Leave the switch/loop
			break;
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}

The default Case

The default case runs when the switch expression doesn't match any of the listed case values, functioning as a catch-all -- useful for handling unexpected input or flagging invalid values explicitly.

Example: The default Case

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare x and set it to 9
	int x = 9;
	// Choose a branch based on the value of x
	switch (x) {
		case 1:
			printf("One");
			// Leave the switch/loop
			break;
		// default runs when no case matches
		default:
			printf("Unknown value");
	}
	// 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 string or a variable as a case label, when labels must be integer constants.
  3. Forgetting the default case for unexpected values.

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.