Learn how to use the C++ switch statement with clear syntax, examples, and real-world decision-making logic. Master switch case in C++ with beginner-friendly explanations and code snippets.
switch Statement?The switch statement is a decision-making statement in C++ that allows a program to select one option from multiple choices.
Real-life example: A university student selects an option from a menu:
Instead of writing many if-else-if conditions, we can use a switch statement.
switch (expression)
{
case value1:
// Statements
break;
case value2:
// Statements
break;
default:
// Statements if no case matches
}
Important keywords
switch: Checks the value of an expression.case: Defines a possible value.break: Ends the switch block and prevents execution from continuing into the next case.default: Executes when no case matches. It is optional.#include <iostream>
using namespace std;
int main()
{
int day;
cout << "Enter a day number (1-3): ";
cin >> day;
switch (day)
{
case 1:
cout << "Monday";
break;
case 2:
cout << "Tuesday";
break;
case 3:
cout << "Wednesday";
break;
default:
cout << "Invalid day number";
}
return 0;
}
Sample output
Enter a day number (1-3): 2
Tuesday
Understand the code
switch (day) checks its value.2, the program displays Tuesday.break exits the switch.default block executes.4.5.1, 4, and 8.A switch statement can select an arithmetic operation.
#include <iostream>
using namespace std;
int main()
{
double num1, num2;
char op;
cout << "Enter first number: ";
cin >> num1;
cout << "Enter operator (+, -, *, /): ";
cin >> op;
cout << "Enter second number: ";
cin >> num2;
switch (op)
{
case '+':
cout << "Result = " << num1 + num2;
break;
case '-':
cout << "Result = " << num1 - num2;
break;
case '*':
cout << "Result = " << num1 * num2;
break;
case '/':
if (num2 != 0)
{
cout << "Result = " << num1 / num2;
}
else
{
cout << "Cannot divide by zero";
}
break;
default:
cout << "Invalid operator";
}
return 0;
}
Sample output
Enter first number: 10
Enter operator (+, -, *, /): *
Enter second number: 5
Result = 50
What to understand
char op stores a single operator.case '+':.if statement inside a switch to prevent division by zero.% using integer variables.num2 = 0.When multiple choices should produce the same output, we can group cases together.
#include <iostream>
using namespace std;
int main()
{
int day;
cout << "Enter day number (1-7): ";
cin >> day;
switch (day)
{
case 1:
case 2:
case 3:
case 4:
case 5:
cout << "Weekday";
break;
case 6:
case 7:
cout << "Weekend";
break;
default:
cout << "Invalid day number";
}
return 0;
}
Sample output
Enter day number (1-7): 6
Weekend
Here, cases 1 through 5 share the same statements. The cases do not contain break until the shared result is reached, so execution continues through the grouped labels.
"Working Day" for cases 1–5."Holiday" for cases 6–7.Menu-driven programs are common in university systems, banking applications and management software.
#include <iostream>
using namespace std;
int main()
{
int choice;
cout << "\n--- Student Management System ---\n";
cout << "1. Student Profile\n";
cout << "2. Semester Result\n";
cout << "3. Fee Status\n";
cout << "4. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice)
{
case 1:
cout << "Displaying student profile.";
break;
case 2:
cout << "Displaying semester result.";
break;
case 3:
cout << "Displaying fee status.";
break;
case 4:
cout << "Exiting the program.";
break;
default:
cout << "Invalid choice.";
}
return 0;
}
Sample output
--- Student Management System ---
1. Student Profile
2. Semester Result
3. Fee Status
4. Exit
Enter your choice: 3
Displaying fee status.
This program demonstrates how a university application could direct users to different functions based on their selection. The current example displays messages rather than retrieving actual student records.
5 for Attendance Status.6 for Course Registration.2, 5, and 9.A switch statement can also select a message based on a character grade.
#include <iostream>
using namespace std;
int main()
{
char grade;
cout << "Enter your grade (A, B, C, D, F): ";
cin >> grade;
switch (grade)
{
case 'A':
cout << "Excellent Performance";
break;
case 'B':
cout << "Very Good Performance";
break;
case 'C':
cout << "Good Performance";
break;
case 'D':
cout << "You Passed";
break;
case 'F':
cout << "You Failed";
break;
default:
cout << "Invalid grade";
}
return 0;
}
Sample output
Enter your grade (A, B, C, D, F): A
Excellent Performance
Important: Character comparisons are case-sensitive. Entering lowercase a will not match uppercase 'A' in this example.
B.W to display "Result Withheld".A, lowercase a, and F.if Inside a switchYou can combine switch with if-else when a menu option requires additional checking.
#include <iostream>
using namespace std;
int main()
{
int choice;
double fee;
cout << "1. Check Fee Status\n";
cout << "2. Exit\n";
cout << "Enter choice: ";
cin >> choice;
switch (choice)
{
case 1:
cout << "Enter fee amount paid: ";
cin >> fee;
if (fee >= 50000)
{
cout << "Fee requirement completed.";
}
else
{
cout << "Remaining fee must be paid.";
}
break;
case 2:
cout << "Goodbye!";
break;
default:
cout << "Invalid choice.";
}
return 0;
}
Sample output
1. Check Fee Status
2. Exit
Enter choice: 1
Enter fee amount paid: 30000
Remaining fee must be paid.
This is a simplified teaching example. In a real university system, the required fee would depend on the student’s actual fee voucher and account balance.
50000 to 60000.50000.switch and if-else-if| Feature | switch |
if-else-if |
|---|---|---|
| Best use | Selecting among discrete values | Checking ranges and complex conditions |
| Example | Menu choice 1, 2 or 3 | Marks greater than or equal to 80 |
| Conditions | Matches case values | Supports relational and logical conditions |
| Range checking | Not directly supported | Supported |
| Data types | Integral types, char, and enum (also certain compatible integral types) |
Conditions that evaluate to true or false |
break |
Commonly used to exit each case | Not required |
default / else |
Optional fallback | Optional fallback |
Remember: switch is suitable for selecting a menu option or matching a grade. Use if-else-if for grade ranges such as 80–89 or for conditions involving multiple comparisons.
Try solving these exercises without copying the examples.
1. Day of the Week
Easy
Input a number from 1 to 7 and display the corresponding day.
2. Simple Calculator
Easy
Input two numbers and an operator. Perform the selected operation.
3. Vowel or Consonant
Medium
Input a character and use switch cases to identify a, e, i, o, or u as vowels. Handle uppercase letters too.
4. University Menu
Medium
Create a menu with options for Profile, Result, Fee Status, Attendance and Exit.
5. Grade Notification
Medium
Input a character grade and display a suitable student notification.
6. Month Days
Advanced
Input a month number and display the number of days. For February, display 28 days and mention that leap years are not considered.
7. ATM Menu
Advanced
Create a menu for Balance Inquiry, Deposit, Withdrawal and Exit. Use switch to process the user’s selection.
break: The program may continue into the following cases.case 'A': for a character and case 1: for an integer.case 50–59: is not valid C++ syntax.default: Without it, an unmatched value may produce no message.float and double are not valid switch controlling types.Ask students to answer these questions:
switch statement?case and default?break?if-else statement inside a switch?if-else-if instead of switch?