Search

Admin

5.3 — Switch statements

Although it is possible to chain many if-else statements together, this is difficult to read. Consider the following program:

Because doing if-else chains on a single variable testing for equality is so common, C++ provides an alternative conditional branching operator called a switch. Here is the same program as above in switch form:

The overall idea behind switch statements is simple: the switch expression is evaluated to produce a value, and each case label is tested against this value for equality. If a case label matches, the statements after the case label are executed. If no case label matches the switch expression, the statements after the default label are executed (if it exists).

Because of the way they are implemented, switch statements are typically more efficient than if-else chains.

Let’s examine each of these concepts in more detail.

Starting a switch

We start a switch statement by using the switch keyword, followed by the expression that we would like to evaluate. Typically this expression is just a single variable, but it can be something more complex like nX + 2 or nX - nY. The one restriction on this expression is that it must evaluate to an integral type (that is, char, short, int, long, long long, or enum). Floating point variables and other non-integral types may not be used here.

Following the switch expression, we declare a block. Inside the block, we use labels to define all of the values we want to test for equality. There are two kinds of labels.

Case labels

The first kind of label is the case label, which is declared using the case keyword, and followed by a constant expression. A constant expression is one that evaluates to a constant value -- in other words, either a literal (such as 5), an enum (such as COLOR_RED), or a constant variable (such as x, when x has been defined as a const int).

The constant expression following the case label is tested for equality against the expression following the switch keyword. If they match, the code under the case label is executed.

It is worth noting that all case label expressions must evaluate to a unique value. That is, you can not do this:

It is possible to have multiple case labels refer to the same statements. The following function uses multiple cases to test if the ‘c’ parameter is an ASCII digit.

In the case where c is an ASCII digit, the first statement after the matching case statement is executed, which is “return true”.

Default labels

The second kind of label is the default label (often called the “default case”), which is declared using the default keyword. The code under this label gets executed if none of the cases match the switch expression. The default label is optional. It is also typically declared as the last label in the switch block, though this is not strictly necessary.

In the isDigit() example above, if c is not an ASCII digit, the default case executes and returns false.

Switch execution and fall-through

One of the trickiest things about case statements is the way in which execution proceeds when a case is matched. When a case is matched (or the default is executed), execution begins at the first statement following that label and continues until one of the following termination conditions is true:
1) The end of the switch block is reached
2) A return statement occurs
3) A goto statement occurs
4) A break statement occurs

Note that if none of these termination conditions are met, cases will overflow into subsequent cases! Consider the following snippet:

This snippet prints the result:

2
3
4
5

This is probably not what we wanted! When execution flows from one case into another case, this is called fall-through. Fall-through is almost never desired by the programmer, so in the rare case where it is, it is common practice to leave a comment stating that the fall-through is intentional.

Break statements

A break statement (declared using the break keyword) tells the compiler that we are done with this switch (or while, do while, or for loop). After a break statement is encountered, execution continues with the statement after the end of the switch block.

Let’s look at our last example with break statements properly inserted:

Now, when case 2 matches, the integer 2 will be output, and the break statement will cause the switch to terminate. The other cases are skipped.

Warning: Forgetting the break statement at the end of the case statements is one of the most common C++ mistakes made!

Declaring variables inside cases

One other bit of weirdness about switches is that you can have multiple statements underneath each case without defining a new block. However, you are not allowed to define new variables in these statements:

If a case needs to define a new variable, you can do so by putting it inside a block underneath the case:

Quiz

1) Write a function called calculate() that takes two integers and a char representing one of the following mathematical operations: +, -, /, or *. Use a switch statement to perform the appropriate mathematical operation on the integers, and return the result. If an invalid operator is passed into the function, the function should print an error.

2) Define an enum (or enum class, if using a C++11 capable compiler) named Animal that contains the following animals: pig, chicken, goat, cat, dog, ostrich. Write a function named getAnimalName() that takes an Animal parameter and uses a switch statement to return the name for that animal as a std::string. Write another function named printNumberOfLegs that uses a switch statement to print the number of legs each animal walks on. Make sure both functions have a default case that prints an error message. Call printNumberOfLegs() from main() with a cat and a chicken. Your output should look like this:

A cat has 4 legs.
A chicken has 2 legs.

Quiz answers

1) Show Solution

2) Show Solution

5.4 -- Goto statements
Index
5.2 -- If statements

48 comments to 5.3 — Switch statements

  • Allen01

    Alex, could you show one or two programs from the examples above worked fully? for example, could you show how the code below might evaluate in the main function. Thanks. Also, shouldn’t the symbolic constant enumerators in the “enum Colors” block end with a ‘,’ instead of a ‘;’?

    enum Colors
    {
        COLOR_BLACK;
        COLOR_WHITE;
        COLOR_RED;
        COLOR_GREEN;
        COLOR_BLUE;
    };
    
    
    
    void PrintColor(Colors eColor)
    {
        using namespace std;
        switch (eColor)
        {
            case COLOR_BLACK:
                cout < < "Black";
                break;
            case COLOR_WHITE:
                cout << "White";
                break;
            case COLOR_RED:
                cout << "Red";
                break;
            case COLOR_GREEN:
                cout << "Green";
                break;
            case COLOR_BLUE:
                cout << "Blue";
                break;
            default:
                cout << "Unknown";
                break;
        }
    }
    
    int Main()
    {
    
    // Please show how the code in the above blocks might evaluate.
    
    }
    
    
    • You’re right, the enums should be separated by commas, not semicolons. I fixed the example.

      Here’s a piece of sample code:

      This produces the result:

      Not the most useful example, perhaps.

      Here’s another example:

      Sample output:

  • Allen01

    Thanks for the examples. I have another question: What are the rules for adding a semi-colon to the end of a block? If I remove the semi-colon from the end of the enum Colors block, it will not compile.

    • As far as I can tell, all statements should be terminated by a semicolon, EXCEPT for compound statements (blocks).

      A declaration is considered a statement, and thus should be terminated by a semicolon. An enum is a declaration so it needs to be terminated with a semicolon. Same with a class declaration. The statements inside a switch are considered compound statements, so they do not need a terminating semicolon.

  • Jeff

    In a real-world application, where we are concerned with the possible perils of integer division, would it be better to have Calculate() return a double? And, if so, does the following work:

    Will the other cases (e.g., addition of two integers) automatically convert the integer sum to a double return value without the compiler complaining?

    • Whether it’s more appropriate to return an int or a double depends entirely on your needs. For some tasks, integer division might be exactly what you want. Others, maybe not. If your function is going to return a double, you might want to modify it to take double parameters as well so you can do floating point addition, subtraction, and multiplication.

      I believe your case would work as you intend. And yes, an integer return value will be implicitly converted to a double if the function has been declared to return a double.

  • siku

    My car class doesn’t compile. Compiler gives me error “C2051: case expression not constant”. Although I have defined a constant integral variable ( const int up = (int) mUpKey). I worked around this problem using if else statements but I am still interested to solve it by using switch statement. Can you explain why switch doesn’t work because I have a constant expression after case.

    Here is the example code:

    class Car
    {
    private:
    char mUpKey;

    public:
    Car(char upKey):mUpKey(upKey){};
    bool getKeyPress( const char c)
    {
    const int up = static_cast(mUpKey);
    switch(c)
    {
    case up:
    //move(0, 1);
    return true;
    }

    return false;
    }
    };

    • Aah, you’ve run across a very interesting case here. Case statements have to be statements that are essentially compile-time constants. So something like this will work:

      This works because up is a constant at compile time.

      However, in your case, your value of up is dependent upon the value of mUpKey, which can vary depending on the instance of Car. So even though your value of up has been made const, it’s not a compile-time constant.

      • siku

        Thanks for your reply. In that case I would not change my code to use switch statement because it would not allow users to set one’s keys later.

  • Anthony

    This code does not give the expected results, any clue why? It compiles, it just doesn’t do what it should do.

    • Anthony

      I updated it with this code, but there has to be a better way, lol

  • Denis

    Is it necessary to use the break statement following the default code? For example -

    It seems rather redundant. Is there a practical purpose for doing so?

    • Alex

      It’s not redundant, it’s superfluous. :)

      The only reason that you’d need to include a break at the end of your default case is if the default case wasn’t the last case. In that case, anything that matched the default case would fall-through to the cases below.

  • Jesper

    Hi Alex

    I want to say that i love these tutorials you’ve made. I’ve been away from
    c++ for some years and I needed a brush-up and this is just perfect. Great job!!
    I have something to add to this section on the switch-case construct though.
    The real reason why there is two ways to achieve the same thing i.e. if-elseif-else and switch-case
    is because of execution-speed. if-elseif-else will always execute in linear time while switch-case
    is implemented using a jump-table meaning it will execute in constant time. Though the jump-table
    will only be used if the case-values are not spread out too far, because that would make the
    jump-table too big.
    So if it is possible you should always use switch-case and keep switch-values in the low range.

  • tadepalli.vinay

    Can someone tell me why switch-case-default statements cannot use floating point values. I mean, i want the reason behind it. As I understand, SWITCH statements can make your program faster compared to the IF-ELSE ladder since it generates a jump table in the compilation without need to wait during execution time. But can’t it be done with floating-point values as well?
    Good Reasoning is required.

  • DrSuse

    Completed (working) code:

  • sush tiwari

    I Love this tutorial!!!!!!!!!! Alex u did a great JOB!!!!!!!!!

    which one is faster switch or if else? why?

    • Alex

      Switch, because of the way it’s implemented internally.

      A switch uses a jump table to quickly determine which set of statements to execute, whereas an if-else chain has to do one comparison per if-else condition.

      However, the downside of switches is that they are somewhat limited in what they can do.

  • What is the differtence between using RETURN or BREAK in the Switch CASE instruction?

    • Alex

      return exits the entire function that the switch is inside of and returns a value to the caller.
      break exits only the switch, and code execution continues below the switch statement.

  • Andy

    I have used every single thing that I learned in previous lessons to complete the quiz.
    Heck, I even used a boolean to check whether the user had already filled in a first operand and it worked.
    Now this is what motivates me to keep learning this awesome language!

    What I didn’t understand is why this gave me an error:

    I had to do this instead:

    If anyone could give me some further explanation on this, that would be highly appreciated!
    Note: This was inside a switch statement under main().

    Thank you for the lessons!

    Sincerely
    Andy

    • Snowman

      Hi, if you still wish for someone to help out, please post the full code. I cannot help with the snippet your provided.

      EDIT

      Ok, maybe I can. Functions return a value, switch statements are just complex ifs and ifs do not return a value but rather preform commands. Therefore, the return is not correct because it is trying to send a value nowhere, but the second snippet of cout, break preforms the cout then exits the switch as intended.

      tl;dr: switch statements cannot return a value in main, only in a function with a return type.

    • Alex

      cout prints something to the screen. What would it mean to return this to another function?

  • Snowman

    So I decided to take it a step further. This program takes a calculation as one long string input, can have multiple operators but will not calculate using BODMAS but rather in order of appearance.

    Eg. user enters 4+7*2-1
    Answer is: 21 (4+7=11, 11*2=22, 22-1=21)

    Just made it as a challenge. Used a C++11 command called stof (string to float). Program ignores whitespace.

    • Snowman

      Correction above, line 33 should be:

      Won’t cause an error, but is wrong none the less

  • Aacon

  • Jatn

    I ran the following code for question in code blocks and compiler showed me lots of messages and 2 errors

    The errors were like this
    C:\Documents and Settings\ADMIN\Desktop\Family\C++ Files\problems\main.cpp|68|error: no match for ‘operator<<‘ in ‘std::operator<< <std::char_traits<char> >((* & std::operator<< <char, std::char_traits<char>, std::allocator<char> >((* & std::operator<< <std::char_traits<char> >((* & std::cout), ((const char*)"A "))), (*(const std::basic_string<char>*)(& getAnimalName(Animal)())))), ((const char*)" has ")) << printNumberOfLegs(Cat)’|

    and the other one is

    C:\Documents and Settings\ADMIN\Desktop\Family\C++ Files\problems\main.cpp|69|error: no match for ‘operator<<‘ in ‘std::operator<< <std::char_traits<char> >((* & std::operator<< <char, std::char_traits<char>, std::allocator<char> >((* & std::operator<< <std::char_traits<char> >((* & std::cout), ((const char*)"A "))), (*(const std::basic_string<char>*)(& getAnimalName(Animal)())))), ((const char*)" has ")) << printNumberOfLegs(Chicken)’|

    Can anybody help….

    • Alex

      printNumberOfLegs() returns void, but you’re trying to send that void result into cout. You’ll need to break line 68 and 69 up like this:

      Or change printNumberOfLegs() to return a value.

  • Todd

    Typo:

    "Write a function called Calculate()" (in your solution, you write a function called ‘calculate()’, which is correct with first letter lowercase)

    Quiz 1 solution typos:

    Above, "Calculate():" should be "calculate():"

    Your main() function doesn’t have a ‘return’ statement.

    Quiz 2 solution typos:

    Above, instead of " walks on ", your quiz question requires us to write " has "

    Above, you should say "printNumberofLegs()", not "getNumberOfLegs()".
    Also above, ‘animal’ (of data type "Animal") cannot be printed here using std::cout (compiler error occurs). Maybe use stat_cast<int>(animal)

  • C++ newbie

    Hey there,

    in my code I had something like this in main:

  • devkkp

    THIS ERROR IS SEEN WHEN I COMPILED A PROGRAM ! WHAT DOES IT MEAN?

    1>c:\users\ganeshji\documents\visual studio 2010\projects\switch statements\switch statements\switch statements.cpp(61): fatal error C1010: unexpected end of file while looking for precompiled header. Did you forget to add ‘#include "StdAfx.h"’ to your source?

  • cpplx

    quiz1… works properly but prints extra nonsense data in the error case. i object :)

  • splotz

    How could we change Quiz 2 to where the user inputs the animal name ?

    • Alex

      Dealing with text is complicated.

      Probably the easiest way would be to add code to do the following:
      * #include <string>
      * Instantiate a variable of type std::string in main().
      * Use std::cin to read in a string from the user
      * Write a function that takes the string as a parameter and returns the appropriate matching enum
      * Ensure your function works with different capitalizations of the string.

  • Shivam Tripathi

    @ If a case needs to define a new variable, you can do so by putting it inside a block underneath the case:

    switch (1)
    {
        case 1:
        { // note addition of block here
            int x = 4; // now legal
            std::cout << x;
            break;
        }
        default:
            std::cout << "default case" << std::endl;
            break;
    }

    Correct me if m wrong…but i think that the "break" should come outside of the block…

  • Shivam Tripathi

    got it…thnx once again Alex…:)

  • techsavvy....aye

    Will you give an example of operating this snippet,

    Thanks in advance.

    • Alex

      Sorry, I’m not clear on what you mean. Are you asking for this snippet to be put inside a program that you can run?

      If so, by this point you should be able to do that yourself. This snippet is self-contained, so you should be able to toss it into an empty main() function and run the resulting program.

Leave a Comment

  

  

  

two × 3 =

Put C++ code inside [code][/code] tags to use the syntax highlighter