Switch Statements

Learn an alternative way to write conditionals, which is often used when we want to take different paths based on a specific value
This lesson is part of the course:

Intro to C++ Programming

Become a software engineer with C++. Starting from the basics, we guide you step by step along the way

Free, Unlimited Access
3D art showing a plumber repairing leaky pipes
Ryan McCombe
Ryan McCombe
Updated

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:

  • Understanding Switch Statements: Grasp how switch statements offer an alternative to traditional if-else chains, especially when dealing with specific variable values.
  • Implementing Cases and Breaks: Learn to use case and break keywords to control the flow of your program.
  • Exploring Fallthrough Logic: Discover how fallthrough works in switch statements and how it can be both useful and tricky.
  • Utilizing the Default Case: Find out how the 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.

An Example Switch Statement

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

Fallthrough

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.

Preventing Fallthrough with 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 breaks to our switch statements.

Adding a default Case

Switch 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
Test your Knowledge

Using switch Statements

What 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";
  }
}

Summary

Congratulations on completing this lesson on switch statements! Here's a quick recap of what we've covered:

  • Switch Statement Basics: You've learned how switch statements provide a streamlined way to handle multiple conditions based on the value of a variable.
  • The case and break Keywords: We explored how to use case and break to control the flow within switch statements.
  • Understanding Fallthrough: You now understand how fallthrough works and its potential as both a useful feature and a source of bugs.
  • The default Case: We discussed the importance of the default case as a catch-all mechanism.

Up Next: Function Return Types and Values

In our next lesson, we'll return to our exploration of functions. You'll learn about:

  • Function Returns: Understanding how functions can return values to their caller.
  • Function Returns and Conditional Logic: Understanding how function returns interact with the conditional logic we learned in the previous two lessons
  • Exit Codes: Be introduced to the mechanism that allows our main function to communicate its status to the operating system whenever our program ends

Was this lesson useful?

Next Lesson

Function return Statements

Allow our functions to communicate with their caller by returning values when they complete
3D art showing a blacksmith character
Ryan McCombe
Ryan McCombe
Updated
Lesson Contents

Switch Statements

Learn an alternative way to write conditionals, which is often used when we want to take different paths based on a specific value

3D art showing a progammer setting up a development environment
This lesson is part of the course:

Intro to C++ Programming

Become a software engineer with C++. Starting from the basics, we guide you step by step along the way

Free, Unlimited Access
Functions, Conditionals and Loops
3D art showing a progammer setting up a development environment
This lesson is part of the course:

Intro to C++ Programming

Become a software engineer with C++. Starting from the basics, we guide you step by step along the way

Free, unlimited access

This course includes:

  • 59 Lessons
  • Over 200 Quiz Questions
  • 95% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Next Lesson

Function return Statements

Allow our functions to communicate with their caller by returning values when they complete
3D art showing a blacksmith character
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved