In this lesson, we focus on switch statements. These are another way to write conditional logic. acting as an alternative to the if
, else
, and if else
statements we covered in the previous lesson.
This section will guide you through:
case
and break
keywords to control the flow of your program.default
case acts as a catch-all for any unhandled cases.By the end of this lesson, you'll be equipped to use switch statements effectively in your C++ programs.
Switch statements are best introduced by an example so below, we have a program that logs out different strings based on the value of an integer Day
variable:
#include <iostream>
using namespace std;
int Day{3};
int main(){
switch (Day) {
case 1: // Is Day == 1?
cout << "Monday";
break;
case 2: // Is Day == 2?
cout << "Tuesday";
break;
case 3: // Is Day == 3?
cout << "Wednesday";
break;
}
}
In this example, we’re switching on the value of Day
. Its value is 3
, so we trigger the case 3:
scenario, logging out Wednesday
:
Wednesday
Sometimes, we want behaviour to trigger if our variable has one of multiple values, somewhat similar to behavior we can implement using the boolean ||
operator.
In the following switch statement, we log out "Weekend"
if the value of Day
is either 6
or 7
:
#include <iostream>
using namespace std;
int Day{6};
int main(){
switch (Day) {
case 1:
cout << "Monday";
break;
case 2:
cout << "Tuesday";
break;
case 6:
case 7:
cout << "Weekend";
}
}
In this program, the value of Day
was 6
, so we got the expected output:
Weekend
This is an example of fallthrough, a quirky characteristic of switch statements. In this case, the fallthrough was desired, but it’s often a source of bugs.
break
The inclusion of the break
keyword in the previous examples is to prevent fallthrough, which is the default behavior of switch statements.
Let's take a look at what happens if we don’t include the break
statements:
#include <iostream>
using namespace std;
int Day{1};
int main(){
switch (Day) {
case 1:
cout << "Monday";
case 2:
cout << "Tuesday";
case 3:
cout << "Wednesday";
}
}
Once a case
statement is triggered, every subsequent case
statement is also triggered, until we either reach the end of the switch
statement or encounter a break
instruction.
In this scenario, because the first case
was triggered, and we have no break
statements, the code in every case was executed. This generated the following output:
MondayTuesdayWednesday
Fallthrough behavior is rarely useful, but it remains the default implementation of switch statements in C++, and most other programming languages.
So, we should be mindful of it and generally remember that, in most scenarios, we’ll need to add break
s to our switch
statements.
default
CaseSwitch statements can have a default
case, which is activated in all scenarios unless a previously activated case triggered a break
:
#include <iostream>
using namespace std;
int Day{3};
int main(){
switch (Day) {
case 1:
cout << "Monday";
break;
case 2:
cout << "Tuesday";
break;
default:
cout << "Something else";
}
}
Something else
switch
StatementsWhat will the following program output?
#include <iostream>
using namespace std;
int Day{1};
int main(){
switch (Day) {
case 1:
cout << "Monday";
case 2:
cout << "Tuesday";
break;
default:
cout << "Something else";
}
}
Congratulations on completing this lesson on switch statements! Here's a quick recap of what we've covered:
case
and break
Keywords: We explored how to use case
and break
to control the flow within switch statements.default
Case: We discussed the importance of the default
case as a catch-all mechanism.In our next lesson, we'll return to our exploration of functions. You'll learn about:
main
function to communicate its status to the operating system whenever our program endsLearn an alternative way to write conditionals, which is often used when we want to take different paths based on a specific value
Become a software engineer with C++. Starting from the basics, we guide you step by step along the way