FAQ: Conditionals and Control Flow - Switch Statement

This community-built FAQ covers the “Switch Statement” exercise from the lesson “Conditionals and Control Flow”.

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

Learn Java

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!
You can also find further discussion and get answers to your questions over in #get-help.

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

Need broader help or resources? Head to #get-help and #community:tips-and-resources. If you are wanting feedback or inspiration for a project, check out #project.

Looking for motivation to keep learning? Join our wider discussions in #community

Learn more about how to use this guide.

Found a bug? Report it online, or post in #community:Codecademy-Bug-Reporting

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!

I am troubled by this exercise pls somebody help me.
https://www.codecademy.com/courses/learn-java/lessons/java-conditionals-and-control-flow/exercises/switch?action=resume_content_item

Hey @yokel2473029837, welcome to the forums!

Would you please look at this topic and add a little bit more to your question?

  
  public double calculateShipping() {
    double shippingCost;
	 	// declare switch statement here
    switch (shipping) {
      case "Regular":
        shippingCost = 0;
        break;
      case "Express":
        shippingCost = 1.75;
        break;
      default:
        shippingCost = 0.50;
    }
    
    return shippingCost;
 }
  

My solution for the exercise 5/6
The reason I used the “shipping” variable inside the paranthesis of the switch block is because…

when we create/instantiate the objects “book” and “chemistrySet” in the main() method…
the shipping preferences “Regular” and “Express” are stored in the “shipping” variable
( you can see this in the constructor method).

Why do we need the break in this case? Can someone explain more about break? Why don’t the switch just stop by itself? Thanks!

Essentially, a switch will match an expression’s value to a case clause, and return the associated statement.

switch (expression) {
  case 1:
    // statement to be executed
  case 2: 
    // statement to be executed
  default:
    // default statement to be executed
}

When it finds what it’s looking for, it gives you back the information that you need.

But what if you had hundreds of cases? You’d have to go through all of them, every time.

Let’s say that you have 500 cases in your switch. And case 1 matches the given expression.

Well you’d still have to go through all other 499 cases. Just to return the statement of case 1.

That’s not efficient at all.

So we add break statements to break out of the switch and return the information we were looking for.

switch (expression) {
  case 1:
    // statement to be executed
    break;
  case 2: 
    // statement to be executed
    break;
  ...
  case 500:
    // statement to be executed
    break;
  default:
    // default statement to be executed
}

If case 1 matches the expression’s value, it will break and return the associated statement, without going through all the other cases. 499 of them in this example.

Much more efficient.

By default the switch won’t just stop by itself. It’s not that smart, you have to tell the program what to do in each situation. It doesn’t think, it executes commands.

Hope this answers your question :wink:

Thanks! That’s really easy to understand. I have one more question: is there any case that the dev get creative and doesn’t use break? Maybe to return more than one statement?