The switch statement is used when a program needs to choose one block of code from multiple possible choices based on the value of an expression.
The switch statement provides another way to perform multi-way decision making in C++.
switch (expression) {
case value1:
// statements
break;
case value2:
// statements
break;
default:
// statements
}
A switch statement can make code easier to read when the same expression needs to be compared against several fixed values.
For example:
A switch statement can handle these choices clearly.
switch (expression) {
case value1:
statement;
break;
case value2:
statement;
break;
default:
statement;
}
The expression is evaluated and compared with the values specified in the case labels.
The case keyword defines a possible value for the switch expression.
int choice = 2;
switch (choice) {
case 1:
std::cout << "Option 1";
break;
case 2:
std::cout << "Option 2";
break;
}
Since the value of choice is 2, the second case executes.
The break statement stops the switch statement after a matching case has executed.
int number = 1;
switch (number) {
case 1:
std::cout << "One";
break;
case 2:
std::cout << "Two";
break;
}
After displaying One, the break statement exits the switch.
The default case executes when none of the case values match the switch expression.
int choice = 5;
switch (choice) {
case 1:
std::cout << "One";
break;
case 2:
std::cout << "Two";
break;
default:
std::cout << "Invalid choice";
}
int day = 3;
switch (day) {
case 1:
std::cout << "Monday";
break;
case 2:
std::cout << "Tuesday";
break;
case 3:
std::cout << "Wednesday";
break;
default:
std::cout << "Invalid day";
}
The output is Wednesday.
#include <iostream>
int main() {
int choice;
std::cout << "Enter choice: ";
std::cin >> choice;
switch (choice) {
case 1:
std::cout << "You selected option 1";
break;
case 2:
std::cout << "You selected option 2";
break;
default:
std::cout << "Invalid choice";
}
return 0;
}
A switch can also be used with a character value.
char grade = 'A';
switch (grade) {
case 'A':
std::cout << "Excellent";
break;
case 'B':
std::cout << "Very Good";
break;
case 'C':
std::cout << "Good";
break;
default:
std::cout << "Other grade";
}
A switch statement can contain many case labels.
int number = 4;
switch (number) {
case 1:
std::cout << "One";
break;
case 2:
std::cout << "Two";
break;
case 3:
std::cout << "Three";
break;
case 4:
std::cout << "Four";
break;
default:
std::cout << "Other number";
}
If break is omitted, execution can continue into the following case statements. This behavior is called fall-through.
int number = 1;
switch (number) {
case 1:
std::cout << "One";
case 2:
std::cout << "Two";
}
Here, after case 1 executes, execution continues into case 2 because there is no break between them.
Sometimes multiple cases can intentionally share the same code.
int day = 6;
switch (day) {
case 6:
case 7:
std::cout << "Weekend";
break;
default:
std::cout << "Working day";
}
Both 6 and 7 execute the same block.
int day = 5;
switch (day) {
case 1:
std::cout << "Monday";
break;
case 2:
std::cout << "Tuesday";
break;
case 3:
std::cout << "Wednesday";
break;
case 4:
std::cout << "Thursday";
break;
case 5:
std::cout << "Friday";
break;
case 6:
std::cout << "Saturday";
break;
case 7:
std::cout << "Sunday";
break;
default:
std::cout << "Invalid day";
}
int month = 2;
switch (month) {
case 1:
std::cout << "January";
break;
case 2:
std::cout << "February";
break;
case 3:
std::cout << "March";
break;
default:
std::cout << "Another month";
}
#include <iostream>
int main() {
char op;
double a, b;
std::cout << "Enter operator (+, -, *, /): ";
std::cin >> op;
std::cout << "Enter two numbers: ";
std::cin >> a >> b;
switch (op) {
case '+':
std::cout << a + b;
break;
case '-':
std::cout << a - b;
break;
case '*':
std::cout << a * b;
break;
case '/':
if (b != 0) {
std::cout << a / b;
}
else {
std::cout << "Cannot divide by zero";
}
break;
default:
std::cout << "Invalid operator";
}
return 0;
}
int choice = 2;
switch (choice) {
case 1:
std::cout << "Add Student";
break;
case 2:
std::cout << "View Student";
break;
case 3:
std::cout << "Delete Student";
break;
case 4:
std::cout << "Exit";
break;
default:
std::cout << "Invalid choice";
}
A switch can be used with enumeration values.
enum Color {
RED,
GREEN,
BLUE
};
Color color = GREEN;
switch (color) {
case RED:
std::cout << "Red";
break;
case GREEN:
std::cout << "Green";
break;
case BLUE:
std::cout << "Blue";
break;
}
Case labels must represent constant values known as case values.
const int ONE = 1;
const int TWO = 2;
int choice = 2;
switch (choice) {
case ONE:
std::cout << "One";
break;
case TWO:
std::cout << "Two";
break;
}
int number = 10;
switch (number) {
case 5:
std::cout << "Five";
break;
case 10:
std::cout << "Ten";
break;
case 15:
std::cout << "Fifteen";
break;
default:
std::cout << "Other value";
}
Integer values are commonly used with switch statements.
An if-else ladder is useful for ranges and complex conditions. A switch is convenient when comparing one expression with specific fixed values.
Using if-else:
if (choice == 1) {
std::cout << "One";
}
else if (choice == 2) {
std::cout << "Two";
}
Using switch:
switch (choice) {
case 1:
std::cout << "One";
break;
case 2:
std::cout << "Two";
break;
}
A traditional C++ switch does not directly switch on
std::string. For string comparisons, an
if-else structure is commonly used.
std::string role = "admin";
if (role == "admin") {
std::cout << "Admin";
}
else if (role == "student") {
std::cout << "Student";
}
This is different from switching on an integer or character.
A switch statement can contain another switch statement.
int category = 1;
int choice = 2;
switch (category) {
case 1:
switch (choice) {
case 1:
std::cout << "Option 1";
break;
case 2:
std::cout << "Option 2";
break;
default:
std::cout << "Invalid option";
}
break;
default:
std::cout << "Invalid category";
}
Nested switches should be used carefully because too much nesting can make programs harder to read.
#include <iostream>
int main() {
int choice;
std::cout << "1. Profile" << std::endl;
std::cout << "2. Attendance" << std::endl;
std::cout << "3. Fees" << std::endl;
std::cout << "4. Logout" << std::endl;
std::cout << "Enter choice: ";
std::cin >> choice;
switch (choice) {
case 1:
std::cout << "Opening Profile";
break;
case 2:
std::cout << "Opening Attendance";
break;
case 3:
std::cout << "Opening Fees";
break;
case 4:
std::cout << "Logging out";
break;
default:
std::cout << "Invalid choice";
}
return 0;
}
#include <iostream>
int main() {
int choice;
std::cout << "1. Check Balance" << std::endl;
std::cout << "2. Deposit" << std::endl;
std::cout << "3. Withdraw" << std::endl;
std::cout << "4. Exit" << std::endl;
std::cout << "Enter choice: ";
std::cin >> choice;
switch (choice) {
case 1:
std::cout << "Checking balance";
break;
case 2:
std::cout << "Deposit selected";
break;
case 3:
std::cout << "Withdraw selected";
break;
case 4:
std::cout << "Exit";
break;
default:
std::cout << "Invalid choice";
}
return 0;
}
char signal = 'R';
switch (signal) {
case 'R':
std::cout << "Stop";
break;
case 'Y':
std::cout << "Wait";
break;
case 'G':
std::cout << "Go";
break;
default:
std::cout << "Invalid signal";
}
std::string.Correct case syntax:
case 1:
std::cout << "Option 1";
break;
#include <iostream>
int main() {
double num1, num2;
char operation;
std::cout << "Enter first number: ";
std::cin >> num1;
std::cout << "Enter operator (+, -, *, /): ";
std::cin >> operation;
std::cout << "Enter second number: ";
std::cin >> num2;
switch (operation) {
case '+':
std::cout << "Result = " << num1 + num2;
break;
case '-':
std::cout << "Result = " << num1 - num2;
break;
case '*':
std::cout << "Result = " << num1 * num2;
break;
case '/':
if (num2 != 0) {
std::cout << "Result = " << num1 / num2;
}
else {
std::cout << "Cannot divide by zero";
}
break;
default:
std::cout << "Invalid operator";
}
return 0;
}
A switch is useful when:
For ranges such as marks >= 90, an if-else ladder is
usually more suitable.
switch (choice) {
case 1:
// action
break;
case 2:
// action
break;
default:
// invalid choice
}
| Part | Purpose |
|---|---|
switch |
Starts the multi-choice selection. |
case |
Defines a possible matching value. |
break |
Exits the switch after a case executes. |
default |
Runs when no case matches. |
| Fall-through | Occurs when execution continues into the next case without a break. |
switch (expression) {
case value1:
// statements
break;
case value2:
// statements
break;
default:
// default statements
}
std::string values.Question: Which keyword is normally used to stop execution after a matching case in a switch statement?