3

I find the PowerScript's CHOOSE CASE statement very useful, as it make so that the code is more clearly than a lot of ifs and else ifs.

Here a example of how it works, from the above link:

CHOOSE CASE weight
   CASE IS < 16
        Postage=Weight*0.30
        Method="USPS"
   CASE 16 to 48
        Postage=4.50
        Method="UPS"
   CASE ELSE
        Postage=25.00
        Method="FedEx"
END CHOOSE

a CASE 5 to 11 is the same as CASE 5, 6, 7, 8, 9, 10, 11

Note that the CHOOSE CASE is not equivalent to java's switch

Philipi Willemann
  • 658
  • 1
  • 9
  • 25
  • No. http://stackoverflow.com/questions/16534025/switch-case-logical-expression-statement-in-java-versus-js-or-php/16534063#16534063 – zw324 May 21 '13 at 17:37

5 Answers5

5

In Java, you can use multiple case statements, but there isn't a nice way to specify an expression as the case qualifier, just literals:

switch(weight) {
  case 1:
  case 2:
  case 3:
    postage = weight * 0.30;
    method = "USPS";
    break;
  case 4:
  case 5:
  case 6:
    postage = 4.5;
    method = "UPS";
    break;
  default:
    postage = 25.0;
    method = "FedEx";
    break;
}

To get nice ranges, stick with if/else:

if(weight > 0 && weight <= 3) {
  postage = weight * 0.30;
  method = "USPS";
}
else if(weight > 3 && weight <= 6) {
  postage = 4.5;
  method = "UPS";
}
else {
  postage = 25.0;
  method = "FedEx";
}
Jonathon Faust
  • 12,396
  • 4
  • 50
  • 63
4

If your objective is cleaning up the decision point, you could encapsulate the code that decides what case applies separately from the code that uses that decision, as in:

enum WeightClass { LOW, MEDIUM, HIGH };

public WeightClass determineWeightClass(int weight)
{
    return (weight < 16) 
        ? WeightClass.LOW 
        : (weight <= 48 
            ? WeightClass.MEDIUM 
            : WeightClass.HIGH);
}

And at the decision point:

switch(determineWeightClass(weight))
{
    case LOW:
        ...
        break;
    case MEDIUM:
        ...
        break;
    case HIGH:
        ...
        break;
}
Jim Garrison
  • 85,615
  • 20
  • 155
  • 190
3

Not exactly the same. If you want to implement such fragment in Java, you have to use if-else[-if] statement.

Basically, it should look like this:

if (weight < 16) {
   //something
} else if (weight >= 16 && weight <= 48) {
   //something else 
} else {
   //some other thing
}

Hope it works for you. :)

Konstantin Yovkov
  • 62,134
  • 8
  • 100
  • 147
2

If there are only 3 cases, a series of if/else is fine. If you have many conditions, you could use a Navigable map and couple it with an enum for a nice and slick design:

public class Test1 {

    public static void main(String[] args) {
        printDelivery(0);
        printDelivery(5);
        printDelivery(16);
        printDelivery(48);
        printDelivery(50);
    }

    private static void printDelivery(int weight) {
        Delivery d = Delivery.getDelivery(weight);
        System.out.println("Weight: " + weight + " => $" + d.getPostage(weight) + " with " + d.getMethod());
    }

    static enum Delivery {
        LOW_WEIGHT(15) {
             public double getPostage(int weight) { return 0.3 * weight; }
             public String getMethod() { return "USPS"; }
        }, MEDIUM_WEIGHT(47) {
            public double getPostage(int weight) { return 4.5; }
            public String getMethod() { return "UPS"; }
        }, HIGH_WEIGHT(Integer.MAX_VALUE){
            public double getPostage(int weight) { return 25.0; }
            public String getMethod() { return "FedEx"; }
        };
        private static final NavigableMap<Integer, Delivery> deliveries = new TreeMap<> ();
        static {
            for (Delivery e : values()) {
                deliveries.put(e.maxWeight, e);
            }
        }
        private final int maxWeight;
        Delivery(int maxWeight) {
            this.maxWeight = maxWeight;
        }
        public static Delivery getDelivery(int weight) {
            return deliveries.ceilingEntry(weight).getValue();
        }

        abstract double getPostage(int weight);
        abstract String getMethod();
    }
}
assylias
  • 321,522
  • 82
  • 660
  • 783
0

No. You would have to use a series of if-elseif-else statements.

Chris Chambers
  • 1,367
  • 21
  • 39