C++ switch...case
statement
The switch...case
statement in C++ provides a simpler and more readable way to handle multiple conditions compared to a series of if...else
statements. It allows you to test a variable or expression against multiple possible values and execute different blocks of code based on the value.
In this blog post, we’ll cover:
switch...case
statement.switch
with different types of values (integer, char).switch...case
in C++.switch...case
and if...else
.switch...case
StatementThe general syntax of the switch
statement is as follows:
switch (expression) {
case constant1:
// Code to execute if expression == constant1
break;
case constant2:
// Code to execute if expression == constant2
break;
case constant3:
// Code to execute if expression == constant3
break;
default:
// Code to execute if none of the above cases match
}
switch
expression: The value or variable that is tested.case
labels: Each case
compares the value of the switch
expression to a constant. If a match is found, the corresponding code block is executed.break
statement: Prevents "fall-through" behavior and exits the switch
statement once a case is matched.default
case: The optional default
case is executed if no case
matches the expression. It's similar to the else
in an if...else
statement.switch...case
StatementLet’s look at a basic example of using switch...case
in C++.
switch...case
Usage
#include <iostream>
using namespace std;
int main() {
int number;
cout << "Enter a number between 1 and 5: ";
cin >> number;
switch (number) {
case 1:
cout << "You entered one." << endl;
break;
case 2:
cout << "You entered two." << endl;
break;
case 3:
cout << "You entered three." << endl;
break;
case 4:
cout << "You entered four." << endl;
break;
case 5:
cout << "You entered five." << endl;
break;
default:
cout << "Invalid number!" << endl;
break;
}
return 0;
}
Output:
Enter a number between 1 and 5: 3
You entered three.
Explanation:
number
against the values in each case
(1, 2, 3, 4, 5).number
is 3
, it matches the case 3
, and the corresponding block of code is executed.break
statement ensures that the program exits the switch
block after executing the code for the matched case.case
labels, the default
case is executed.switch
with char
Data TypeThe switch...case
statement in C++ can also be used with char
values. The character is internally treated as its ASCII value.
switch
with char
#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!" << endl;
break;
case 'B':
cout << "Good!" << endl;
break;
case 'C':
cout << "Average!" << endl;
break;
case 'D':
cout << "Below average!" << endl;
break;
case 'F':
cout << "Fail!" << endl;
break;
default:
cout << "Invalid grade!" << endl;
break;
}
return 0;
}
Output:
Enter your grade (A, B, C, D, F): B
Good!
Explanation:
grade
(a char
) against each case
label.B
, the corresponding block prints "Good!".default
case handles invalid inputs that aren't A
, B
, C
, D
, or F
.switch
In C++, if you omit the break
statement in a case
, control "falls through" to the next case
, even if that case doesn't match the expression. This behavior can sometimes be useful, but it is generally discouraged because it can lead to logical errors.
switch
#include <iostream>
using namespace std;
int main() {
int number = 2;
switch (number) {
case 1:
cout << "One" << endl;
case 2:
cout << "Two" << endl;
case 3:
cout << "Three" << endl;
default:
cout << "Invalid" << endl;
}
return 0;
}
Output:
Two
Three
Invalid
Explanation:
switch
starts by checking number == 2
, and since there’s no break
after case 1
, control falls through to case 2
, then case 3
, and finally the default
case.break
after each case unless fall-through is deliberately required.case
Labels for the Same CodeYou can group multiple case
labels together to execute the same block of code for different values.
case
Labels
#include <iostream>
using namespace std;
int main() {
int number;
cout << "Enter a number between 1 and 3: ";
cin >> number;
switch (number) {
case 1:
case 2:
cout << "You entered one or two." << endl;
break;
case 3:
cout << "You entered three." << endl;
break;
default:
cout << "Invalid number!" << endl;
break;
}
return 0;
}
Output:
Enter a number between 1 and 3: 2
You entered one or two.
Explanation:
case 1
and case 2
will execute the same block of code, printing "You entered one or two.".break
ensures that the control exits the switch
statement after that.switch...case
and if...else
Feature | switch...case |
if...else |
---|---|---|
Expression type | Works with constants (integers, characters, etc.). | Works with expressions of any type (boolean, integer, etc.). |
Use cases | Best for comparing a variable against multiple values of the same type. | Suitable for more complex conditions or when working with ranges. |
Performance | Generally faster when dealing with many conditions. | More flexible but can be less efficient with many conditions. |