FAQ: Conditionals & Logic - Switch Statement

This community-built FAQ covers the “Switch Statement” exercise from the lesson “Conditionals & Logic”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn C++

FAQs on the exercise Switch Statement

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

1 Like

The content says: Note: Without the break keyword at the end of each case, the program would execute the code for all matching cases and the default code as well. This behavior is different from if / else conditional statements which execute only one block of code.

Is this correct? without the ‘break’ keyword, i don’t think the program executes the code for “all matching cases AND the default case”, rather, it simply executes “all the code within that matching case and below”. For example, if you let number = 1 in the example, it will execute cases of 1, 2, 3, 4, and 5 because case 5 has ‘break’ keyword in it. Note that number is clearly NOT 2 nor 3 nor 4 nor 5. so case 2, case 3, case 4, case 5 are NOT matching case at all.

1 Like

Hello @mraji. Welcome to the forum!
You are EXACTLY right! I believe this issue has already been reported to Codecademy. I’ll check for sure. If it hasn’t, I’ll report it.

Update: Didn’t see it in the que of active bugs, so I made a report. Thanks for pointing out this error.

1 Like

Can a switch statement be used together with relational operators such as: <, !=, >= please?
For example,

#include <iostream>

int main() {

    int rating = 4;

    switch (rating) {

        case (rating <= 3): // Is this possible please?
            std::cout << "Less than or equal to 3 stars!\n";
        break;
        default:
            std::cout << "Better than 3 stars!\n";
        break;

    }

}

Thank you!

Hello, @shenobey.
I don’t think you can use an expression like that in a case. Someone please correct me if I’m wrong. You can, however, use a range like this:

#include <iostream>

int main() {

    int rating = 4;

    switch (rating) {

        case 0 ... 3: // You have to include a space then 3 dots, so the compiler knows the dots aren't decimals. The space after the dots is optional. The values are inclusive.
            std::cout << "Less than or equal to 3 stars!\n";
        break;
        default:
            std::cout << "Better than 3 stars!\n";
        break;

    }

}

You can also use ranges of letters: 'A' ... 'M'. The spaces, I believe are optional with non-numeric values, so 'A'...'M' should also work.

1 Like

Thank you very much, midlindner!

1 Like