Introduction
The switch
statement in C++ allows you to select one of many code blocks to be executed based on the value of an expression. It provides a way to handle multiple conditions more efficiently than using a series of if-else-if
statements. This chapter will explain how the switch
statement works, along with examples to demonstrate its use.
Syntax
The basic syntax of a switch
statement is as follows:
switch (expression) {
case constant1:
// code to be executed if expression equals constant1
break;
case constant2:
// code to be executed if expression equals constant2
break;
// additional cases
default:
// code to be executed if expression does not match any case
}
Key Points
- The
expression
is evaluated once and compared with the values of eachcase
. - If a match is found, the corresponding code block is executed.
- The
break
statement is used to exit theswitch
statement once a matching case is executed. Withoutbreak
, the execution will "fall through" to the subsequent cases. - The
default
case is optional and will be executed if none of thecase
values match theexpression
.
Example: Basic switch Statement
#include <iostream>
using namespace std;
int main() {
int day = 3;
switch (day) {
case 1:
cout << "Monday" << endl;
break;
case 2:
cout << "Tuesday" << endl;
break;
case 3:
cout << "Wednesday" << endl;
break;
case 4:
cout << "Thursday" << endl;
break;
case 5:
cout << "Friday" << endl;
break;
case 6:
cout << "Saturday" << endl;
break;
case 7:
cout << "Sunday" << endl;
break;
default:
cout << "Invalid day" << endl;
}
return 0;
}
Output
Wednesday
Example: switch with default Case
In this example, we’ll include a default
case to handle any unexpected values.
#include <iostream>
using namespace std;
int main() {
int score = 85;
switch (score / 10) {
case 10:
case 9:
cout << "Grade: A" << endl;
break;
case 8:
cout << "Grade: B" << endl;
break;
case 7:
cout << "Grade: C" << endl;
break;
case 6:
cout << "Grade: D" << endl;
break;
default:
cout << "Grade: F" << endl;
}
return 0;
}
Output
Grade: B
Example: switch without break (Fall-through)
If you omit the break
statement, the program will continue to execute the subsequent cases until it encounters a break
or reaches the end of the switch
statement.
#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 << "Other" << endl;
}
return 0;
}
Output
Two
Three
Other
In the above example, because there are no break
statements, the execution falls through from case 2
to case 3
and then to the default
case.
Conclusion
The switch
statement in C++ is used for handling multiple conditions based on the value of an expression. It can make your code more readable and efficient compared to multiple if-else-if
statements. This chapter covered the basic syntax, the importance of the break
statement, the use of the default
case, and the concept of fall-through in a switch
statement. Understanding these concepts will help you write better conditional logic in your programs. In the next chapter, we will explore loops in C++, starting with the for
loop.