How to do a case with multiple conditions?

AfroMan picture AfroMan · Dec 8, 2012 · Viewed 54.6k times · Source

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

Answer

Bohemian picture Bohemian · Dec 8, 2012

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.