FAQ: Conditionals and Control Flow - If-Then-Else-If

This community-built FAQ covers the “If-Then-Else-If” 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 If-Then-Else-If

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!

Edit://
I figured it out. I have to return everything.
This is the correct way:

  public double calculateShipping() {
	 	// declare conditional statement here
    if (shipping.equals("Regular")) {
      return 0;
      
    } else if (shipping.equals("Express")) {
      return 1.75;
      
    } else {
      return .50;
    }
    
 	}

 public double calculateShipping() {
	 	// declare conditional statement here
    if (shipping.equals("Regular")) {
      System.out.println("0");
    } else if (shipping.equals("Express")) {
      System.out.println("1.75"); 
    } else {
      System.out.println(".50");
    }
 	}
./Order.java:33: error: missing return statement
 	}

Emm what?

1 Like

Since the method is double -or even if it was int,char etc- it requires a return value cuz it’s method with a certain value
so according to me the code could be :

public  double calculateShipping() {
	 	// declare conditional statement here
    if(shipping.equals("Regular"))
    {
      return 0;
      
    }
    else if(shipping.equals("Express")){
      return 1.75;
    }
    else
    {
      return 0.50;
    }
 	}

In Conditional and Control Flow lesson : if-then else if exercise ,I don t understand why my output doesn’t correspond to the actual order of my code
Here’s my code:

public class Order {
  boolean isFilled;
  double billAmount;
  String shipping;
  
  public Order(boolean filled, double cost, String shippingMethod) {
		if (cost > 24.00) {
      System.out.println("High value item!");
    }
    isFilled = filled;
    billAmount = cost;
    shipping = shippingMethod;
  }
  
  public void ship() {
    if (isFilled) {
      System.out.println("Shipping");
      System.out.println("Shipping cost: " + calculateShipping());
    } else {
      System.out.println("Order not ready");
    }
  }
  
  public double calculateShipping() {
	 	// declare conditional statement here
    if(shipping.equals("Regular"))
    {
      return 0;
      
    }
    else if(shipping.equals("Express")){
      return 1.75;
    }
    else
    {
      return 0.50;
    }
 	}
  
  public static void main(String[] args) {
    // do not alter the main method!
    Order book = new Order(true, 9.99, "Express");
    Order chemistrySet = new Order(false, 72.50, "Regular");
    
    book.ship();
    chemistrySet.ship();
  }
}

Output:

High value item!
Shipping
Shipping cost: 1.75
Order not ready

public class Order {
boolean isFilled;
double billAmount;
String shipping;

public Order(boolean filled, double cost, String shippingMethod) {
if (cost > 24.00) {
System.out.println(“High value item!”);
}
isFilled = filled;
billAmount = cost;
shipping = shippingMethod;
}

public void ship() {
if (isFilled) {
System.out.println(“Shipping”);
System.out.println("Shipping cost: " + calculateShipping());
} else {
System.out.println(“Order not ready”);
}
}

public double calculateShipping() {
// declare conditional statement here
if (shipping.equals(“Regular”)){
return 0;
} else if (shipping.equals(“Express”)) {
return 1.75;
} else {
return 0.50;
}
}

public static void main(String args) {
// do not alter the main method!
Order book = new Order(true, 9.99, “Express”);
Order chemistrySet = new Order(false, 72.50, “Regular”);

book.ship();
chemistrySet.ship();

}
}
Here is my solution. Everything seems fine excepts that the console is printing out the statement “High value item!” and I’m not sure why given that the book cost is 9.99 when the book object is being created/instantiated.

1 Like

yeah I’m confused as well.
The book object is being instantiated/created first, but the console prints out “high value item!” even though the book object has a cost of “9.99” which does not satisfy the if-else statement in the constructor method.

yes correct. We want to return a value, not the string that represents the value.