26

In the 1 month experience I've had with any programming language, I've assumed that switch case conditions would accept anything in the parenthesis as a boolean checking thingamajig, ie these:

|| && < >

Know what I mean?

something like

char someChar = 'w';
switch (someChar) {
case ('W' ||'w'):
    System.out.println ("W or w");
}

Sadly, doesn't seem to work that way. I can't have boolean checking in switch case.

Is there a way around it?

By the way, terribly sorry if I'm sounding confusing. I don't quite know the names for everything in this language yet :X
Any answers appreciated

Sam
  • 7,252
  • 16
  • 46
  • 65
AfroMan
  • 315
  • 1
  • 3
  • 7
  • "boolean checking thingamajig these" are called "operators", and "tis Java" is made clear by the `java` tag you added. Also, the entire code snippet you posted is invalid (Java doesn't have a `case`), and `{` and `}` are misplaced). It's better to post *real code* you're having issues with; most of the time the act of making up fictional code either hides the problem or adds additional problems that aren't in the original. Perhaps a Google search would help - I tried `case statement Java` and the [first result](http://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html) was helpful. – Ken White Dec 08 '12 at 04:34
  • If the answer is right, pick it as accepted – Roger Dec 08 '12 at 04:37
  • Operators. Got it. And I didn't really know if anyone even looked at the tags, thus "tiz java". That snippet was enough to show what I wanted to ask was it not? Thanks for the fixes btw :) – AfroMan Dec 08 '12 at 04:39
  • Just FYI:. First, when you post a comment addressed to someone, address it with `@username` so they see it. You get notified (it's your question), but we don't. Second, tags are really important here. They classify questions into categories so that they're found easily by people who are looking for information, and also for searching for help/answers. And as far as the snippet, please read what I wrote again - the fact it helped you *this time* doesn't mean it will in the future, and the advice I gave you may be very useful at some point. :-) And the same applies about the Google search. – Ken White Dec 08 '12 at 05:05
  • @KenWhite Firstly, thanks for teaching me more about the site. Secondly, I looked through a good bunch of sites including the java website. Keep in mind I'm not that experienced yet and couldn't make heads or tails as to how it could have helped me. It's probably in there somewhere, I'm sure. I hate to bother others for their precious time so they can help me, but I didn't find a good and simple enough answer that satisfied the problem for me. I've asked one question before, and I kept it as meticulous as possible. I just happened to be in a hurry this time so, sorry about that :P – AfroMan Dec 08 '12 at 05:47

5 Answers5

57

You can achieve an OR for cases like this:

switch (someChsr) {
case 'w':
case 'W':
    // some code for 'w' or 'W'
    break;
case 'x': // etc
}

Cases are like a "goto" and multiple gotos can share the same line to start execution.

Jonathan Schneider
  • 26,852
  • 13
  • 75
  • 99
Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • Wow. Fast reply. Works like a charm. Thanks! – AfroMan Dec 08 '12 at 04:27
  • The most clever hack I have ever seen here. Must +1. And another guys asks here in [another question](http://stackoverflow.com/questions/5086322/java-switch-statement-multiple-cases/39123659) and ppl say it's impossible. I have quote your answer there. – WesternGun Aug 24 '16 at 12:43
7

You can do -

switch(c) {
    case 'W':
    case 'w': //your code which will satisfy both cases
              break;

    // ....
}
devang
  • 5,376
  • 7
  • 34
  • 49
4

Every case is normally followed by a "break;" statement to indicate where execution should terminate. If you omit the "break;", then execution will continue. You can use this to support multiple cases which should be handled the same way:

char someChar = 'w';
{
case 'W':
  // no break here
case 'w': 
  System.out.println ("W or w");
  break;
}
JimN
  • 3,120
  • 22
  • 35
  • Yeah I noticed the break thing when it kept going to the next case. Clever way to take care of it. Thanks! – AfroMan Dec 08 '12 at 04:29
1

Switch cases are branches for alternative evaluations of a given expression. The expression is given in the switch parenthesis and can be byte, short, char, and int data types.

The body of a switch statement is known as a switch block. A statement in the switch block can be labeled with one or more case or default labels. The switch statement evaluates its expression, then executes all statements that follow the matching case label.

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html

Aram Kocharyan
  • 20,165
  • 11
  • 81
  • 96
  • Didn't really answer the question (for me) but I now know how far I can go about with the variable types in switch case :) Thanks – AfroMan Dec 08 '12 at 04:31
  • No probs, also note that switch cases have a few intricacies that can be potential gotchas - like the `default` case at the end, and **falling through** one case into another if a `break` or `return` isn't used. Some languages such as Python have excluded them entirely. http://stackoverflow.com/questions/374239/why-doesnt-python-have-a-switch-statement – Aram Kocharyan Dec 08 '12 at 04:36
0

For an alternate to switch statement(multiple if conditions), I think the best solution will be using an enum. For example: Consider the case below:-

    public enum EnumExample {

  OPTION1{

    public double execute() {
      Log.info(CLASS_NAME, "execute", "The is the first option.");
      return void;
    }

  },
  OPTION2{

    public double execute() {
      Log.info(CLASS_NAME, "execute", "The is the second option.");
      return void;
    }

  },
  OPTION3{

    public double execute() {
      Log.info(CLASS_NAME, "execute", "The is the third option.");
      return void;

  };

  public static final String CLASS_NAME = Indicator.class.getName();

  public abstract void execute();

}

The above enum can be used in the following fashion:

EnumExample.OPTION1.execute();

Hopefully this helps you guys.