The switch statement is a decision-making statement in C++ that allows a program to choose one option from multiple possible values. It is often used when a variable can take several discrete values.

  • Purpose: To simplify decision-making when there are many possible cases.

  • Advantage: Cleaner and more readable than using multiple if-else statements.

Syntax of switch Statement

 
switch (expression) {
case value1:
// code to execute if expression equals value1

break;
case value2:
// code to execute if expression equals value2
break; // more
cases
default:
// code to execute if none of the cases match

}

Explanation:

  • expression: A variable or value to evaluate. Must be integer, char, or enum type.

  • case value: Each case is a possible value of the expression.

  • break: Stops execution of the switch after the matching case executes.

  • default: Optional; executes if no case matches.

Example 1: Simple Switch Statement

 
#include <iostream>

using namespace std;
int main() {
int day = 3;
switch (day) {
case 1:
cout << "Monday" << endl;
break;
case 2:
cout << "Tuesday" << endl;
break;
case 3:
cout << "Wednesday" << endl;
break;
default:
cout << "Invalid day" << endl;
}
return 0;
}

Output:

 
Wednesday
  • The day variable equals 3, so case 3 executes.

  • Without break, execution continues to the next case (fall-through behavior).

Example 2: Switch Statement with Characters

 
#include <iostream>
using namespace std;
int main() {
char grade = 'B';
switch (grade) {
case 'A':
cout << "Excellent!" << endl;
break;
case 'B':
cout << "Good!" << endl;
break;
case 'C':
cout << "Average!" << endl;
break;
default:
cout << "Invalid grade" << endl;
}
return 0;
}

Output:

 
Good!
  • Switch works with characters as well as integers.

Using Default Case

  • The default case handles unexpected values.

  • It is optional, but recommended for robust programs.

 
#include <iostream>
using namespace std;
int main() {
int month = 13;
switch (month) {
case 1:
cout << "January" << endl;
break;
case 2:
cout << "February" << endl;
break;
default:
cout << "Invalid month" << endl;
}
return 0;
}

Output:

 
Invalid month

When to Use Switch vs If

FeatureSwitchIf / If-Else
Number of casesBest for many discrete valuesCan handle any number of conditions
Condition typeWorks only with integer, char, enumWorks with any Boolean expression
ReadabilityCleaner for many specific valuesCan become long and complex with many else-if
Fall-through behaviorYes, without breakNo, each if executes independently
FlexibilityLimited to equality checksMore flexible, supports complex conditions

Example: If vs Switch

If-Else Version:

 
#include <iostream>
using namespace std;
int main() {
int day = 2;
if (day == 1)
cout << "Monday" << endl;
elseif (day == 2)
cout << "Tuesday" << endl;
else if (day == 3)
cout << "Wednesday" << endl;
else
cout << "Invalid day" << endl;
return 0;
}

Switch Version:

 
#include <iostream>
using namespace std;
int main() {
int day = 2;
switch(day) {
case 1:
cout << "Monday" << endl;
break;
case 2:
cout << "Tuesday" << endl;
break;
case 3:
cout << "Wednesday" << endl;
break;
default:
cout << "Invalid day" << endl;
}
return 0;
}
  • Both programs give the same output, but switch is easier to read when there are many cases.

Example 3: Real-Life Scenario – Menu Selection

 
#include <iostream>
using namespace std;
int main() {
int choice;
cout << "Menu:\n1. Pizza\n2. Burger\n3. Pasta"<<endl;
cout<<"Enter your choice: ";
cin >> choice;
switch(choice) {
case 1:
cout << "You ordered Pizza." << endl;
break;
case 2:
cout << "You ordered Burger." << endl;
break;
case 3:
cout << "You ordered Pasta." << endl;
break;
default:
cout << "Invalid choice!" << endl;
}
return 0;
}
  • switch is ideal for menus, options, and discrete selections.

Key Points About Switch

  1. Expression must be integer, char, or enum.

  2. Use break to prevent fall-through.

  3. default handles all unmatched cases.

  4. Cannot use ranges or complex conditions in switch.