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.

2 Likes

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.

2 Likes

Thank you very much, midlindner!

1 Like

Why is this the case? In mraji’s example, why would the computer execute case 2 , 3, etc, if number = 1?

Like isn’t the computer supposed to only execute cases that match the condition?

int main() {
int number = ?;
switch(number) {
case 1 :
std::cout << “Bulbusaur\n”;
case 2 :
std::cout << “Ivysaur\n”;
case 3 :
std::cout << “Venusaur\n”;
break;
case 4 :
std::cout << “Charmander\n”;
break;
case 5 :
std::cout << “Charmeleon\n”;
break;
case 6 :
std::cout << “Charizard\n”;
break;
case 7 :
std::cout << “Squirtle\n”;
break;
case 8 :
std::cout << “Wartortle\n”;
break;
case 9 :
std::cout << “Blastoise\n”;
break;
default :
std::cout << “Unknown\n”;
break;
}
}

If i set number to 1 it will still run case 2 and 3 because they don’t have breaks, but if i set number to 3 it will only run case 3, presumably because the computer recognizes that number != case 1 or case 2. Why can the computer recognize the inequality in the latter case but not the former?

Hello @arnodunstatter.

Everything between the { }'s following the switch() statement is a single block of code. The case 1:, case 2:, etc. are just labels. Basically what happens is when the expression inside of the switch() statement’s parenthesis is evaluated, control flow is passed to the matching label. Labels aren’t executed. They just mark a spot in the switch statement’s code block. When control is passed to first matching case: label, every line of executable code that follows inside of the code block gets executed. If control reaches a break; statement it gets passed to the next line of code outside the switch statement’s code block.

3 Likes

hello @midlindner, just a reminder to the previously asked question concerning the absence of the “break”. the bur hasn`t been resolved yet?

Not sure what you mean.

I possibly found a bug in this part. If you type the new cases 7, 8, 9 without the break statement(?) then the code still passes the test.

Here’s the code:

int main() {
  
  int number = 7;
  
  switch(number) {
    
    case 1 :
      std::cout << "Bulbusaur\n";
      break;
    case 2 :
      std::cout << "Ivysaur\n";
      break;
    case 3 :
      std::cout << "Venusaur\n";
      break;
    case 4 :
      std::cout << "Charmander\n";
      break;
    case 5 :
      std::cout << "Charmeleon\n";
      break;
    case 6 :
      std::cout << "Charizard\n";
      break;
    case 7 :
      std::cout << "Squirtle\n";
    case 8 :
      std::cout << "Wartortle\n";
    case 9 :
      std::cout << "Blastoise" << std::endl;
      break;
    default :
      std::cout << "Unknown\n";
      break;
  
  }
  
}

Why the distinction between if statements vs switch statements? Are there different cases within software where one may be better than the other?

So, I’m not sure if this is intentional or not. But on this exercise, even if you type the code the exact way they say to in the hint, or copy and paste it in, it still wont work. They are missing a } in the code.

switch(number) {

case 1 :
  std::cout << "Bulbusaur\n";
  break;
case 2 :
  std::cout << "Ivysaur\n";
  break;
case 3 :
  std::cout << "Venusaur\n";
  break;
case 4 :
  std::cout << "Charmander\n";
  break;
case 5 :
  std::cout << "Charmeleon\n";
  break;
case 6 :
  std::cout << "Charizard\n";
  break;
case 7 :
  std::cout << "Squirtle\n";
  break;
case 8 :
  std::cout << "Wartortle\n";
  break;
case 9 :
  std::cout << "Blastoise\n";
  break;
default :
  std::cout << "Unknown\n";
  break;

} //this is where the bracket is missing. Including in the Hint. Once you place this one, the code will work and you’ll be able to move on.
}

So does the case number correspond to the variable number? Im doing the switch statement lesson and when i run the program it prints the number of the variable and the default. Is that how its suppose to be because when i switch the variable number around it does not print the default.
Here’s the code.

#include

int main() {

int number = 9;

switch(number) {

case 1 :
  std::cout << "Bulbusaur\n";
  break;
case 2 :
  std::cout << "Ivysaur\n";
  break;
case 3 :
  std::cout << "Venusaur\n";
  break;
case 4 :
  std::cout << "Charmander\n";
  break;
case 5 :
  std::cout << "Charmeleon\n";
  break;
case 6 :
  std::cout << "Charizard\n";
  break;
  case 7 :
  std::cout << "Squirtle\n";
  break;
  case 8 :
  std::cout << "Wartortle\n";
  break;
  case 9 :
  std::cout << "Blastoise\n";
default :
  std::cout << "Unknown\n";
  break;

}

}

ah nevermind i forgot the to break case 9. :expressionless: