Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do a case with multiple conditions?

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

like image 216
AfroMan Avatar asked Dec 08 '12 04:12

AfroMan


People also ask

Can CASE have multiple conditions?

Case statement controls the different sets of a statement based upon different conditions. It contains WHEN, THEN & ELSE statements to execute the different results with different comparison operators like =, >, >=, <, <= so on. Case executes through the conditions and returns a result when the condition is true.

How do you add two conditions?

Syntax. SELECT column1, column2, columnN FROM table_name WHERE [condition1] AND [condition2]... AND [conditionN]; You can combine N number of conditions using the AND operator.

How do you do multiple conditions in SQL?

The SQL AND condition and OR condition can be combined to test for multiple conditions in a SELECT, INSERT, UPDATE, or DELETE statement. When combining these conditions, it is important to use parentheses so that the database knows what order to evaluate each condition.


2 Answers

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.

like image 193
Bohemian Avatar answered Sep 21 '22 17:09

Bohemian


You can do -

switch(c) {     case 'W':     case 'w': //your code which will satisfy both cases               break;      // .... } 
like image 21
devang Avatar answered Sep 21 '22 17:09

devang