C++ case Keyword
The case
keyword in C++ is used within a switch statement to define specific actions based on the value of a variable or expression. Each case
represents a distinct scenario, and the program executes the statements associated with the matching case
. The case
keyword is typically followed by a constant value, and its block of code ends with a break statement to prevent fall-through to the next case
.
The switch
statement and case
blocks are often used as an alternative to multiple if-else conditions for better readability and performance.
Syntax
switch (expression) {
case constant1:
// Statements for constant1
break;
case constant2:
// Statements for constant2
break;
...
default:
// Default statements
}
- switch
- The keyword that initiates the
switch
statement, evaluating theexpression
. - case
- Specifies a constant value to match against the
expression
. If matched, the associated block of code executes. - break
- Exits the
switch
statement to prevent fall-through to subsequent cases. - default
- Specifies the block of code to execute if no
case
matches. This is optional.
Examples
Example 1: Basic switch
with case
This example demonstrates how to use the case
keyword to match specific values.
#include <iostream>
using namespace std;
int main() {
int choice = 2;
switch (choice) {
case 1:
cout << "You selected option 1." << endl;
break;
case 2:
cout << "You selected option 2." << endl;
break;
case 3:
cout << "You selected option 3." << endl;
break;
default:
cout << "Invalid option." << endl;
}
return 0;
}
Output:
You selected option 2.
Explanation:
- The variable
choice
is evaluated by theswitch
statement. - Case
2
matches the value ofchoice
, and its associated block of code executes. - The
break
statement prevents execution from continuing into other cases.
Example 2: Fall-Through Behavior Without break
This example demonstrates what happens when the break
statement is omitted in a case
.
#include <iostream>
using namespace std;
int main() {
int number = 1;
switch (number) {
case 1:
cout << "Case 1 executed." << endl;
case 2:
cout << "Case 2 executed." << endl;
case 3:
cout << "Case 3 executed." << endl;
default:
cout << "Default executed." << endl;
}
return 0;
}
Output:
Case 1 executed.
Case 2 executed.
Case 3 executed.
Default executed.
Explanation:
- The variable
number
is evaluated by theswitch
statement. - Case
1
matches, and its block executes. - Since there is no
break
statement, the execution continues into subsequent cases (fall-through behavior). - The program executes all remaining cases and the
default
block.
Example 3: Using case
with Characters
This example shows how the case
keyword can be used with character constants.
#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 << "Fair." << endl;
break;
default:
cout << "Invalid grade." << endl;
}
return 0;
}
Output:
Good!
Explanation:
- The variable
grade
is evaluated by theswitch
statement. - Case
'B'
matches the value ofgrade
, and its associated block of code executes. - The
break
statement prevents further execution of subsequent cases.
Key Points about case
Keyword
- The
case
keyword defines a condition in aswitch
statement. - Each
case
must be followed by a constant expression, such as an integer or character. - The
break
statement is typically used to terminate acase
block and prevent fall-through. - If no
case
matches, the optionaldefault
block executes. - The
case
keyword simplifies complex decision-making logic compared to multipleif-else
statements.