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
switchstatement, evaluating theexpression. - case
- Specifies a constant value to match against the
expression. If matched, the associated block of code executes. - break
- Exits the
switchstatement to prevent fall-through to subsequent cases. - default
- Specifies the block of code to execute if no
casematches. 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
choiceis evaluated by theswitchstatement. - Case
2matches the value ofchoice, and its associated block of code executes. - The
breakstatement 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
numberis evaluated by theswitchstatement. - Case
1matches, and its block executes. - Since there is no
breakstatement, the execution continues into subsequent cases (fall-through behavior). - The program executes all remaining cases and the
defaultblock.
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
gradeis evaluated by theswitchstatement. - Case
'B'matches the value ofgrade, and its associated block of code executes. - The
breakstatement prevents further execution of subsequent cases.
Key Points about case Keyword
- The
casekeyword defines a condition in aswitchstatement. - Each
casemust be followed by a constant expression, such as an integer or character. - The
breakstatement is typically used to terminate acaseblock and prevent fall-through. - If no
casematches, the optionaldefaultblock executes. - The
casekeyword simplifies complex decision-making logic compared to multipleif-elsestatements.
