Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to format multiple 'or' conditions in an if statement

I have an if statement with many conditions (have to check for 10 or 15 constants to see if any of them are present.)

Instead of writing something like:

if (x == 12 || x == 16 || x == 19 || ...) 

is there any way to format it like

if x is [12, 16, 19]? 

Just wondering if there is an easier way to code this, any help appreciated.

The answers have been very helpful, but I was asked to add more detail by a few people, so I will do that to satiate their curiosity. I was making a date validation class that needed to make sure days were not > 30 in the months that have only 30 days (of which there are 4, I think) and I was writing an if statement to check things like this:

if (day > 30 && (month == 4 || month == 6 || month == 9 || month == 11)) 

I was just wondering if there was a faster way to code things like that - many of the answers below have helped.

like image 441
Anthony Avatar asked Sep 30 '11 00:09

Anthony


People also ask

How do you write an if statement with multiple conditions?

When you combine each one of them with an IF statement, they read like this: AND – =IF(AND(Something is True, Something else is True), Value if True, Value if False) OR – =IF(OR(Something is True, Something else is True), Value if True, Value if False) NOT – =IF(NOT(Something is True), Value if True, Value if False)

How do you use Excel if statement with multiple conditions range and or?

To use If and Or statement excel, you need to apply a similar formula as you have applied for If & And with the only difference is that if any of the condition is true then it will show you True.

How do you put 3 conditions in if Excel?

If you have to write an IF statement with 3 outcomes, then you only need to use one nested IF function. The first IF statement will handle the first outcome, while the second one will return the second and the third possible outcomes. Note: If you have Office 365 installed, then you can also use the new IFS function.

Can you have 4 conditions in an if statement?

Here's the secret: the AND formula takes the place of the logical test in the IF formula. Then within the parenthesis of the AND formula, you can write as many conditions (logical tests) as you want, each separated by a comma.


2 Answers

I use this kind of pattern often. It's very compact:

// Define a constant in your class. Use a HashSet for performance private static final Set<Integer> values = new HashSet<Integer>(Arrays.asList(12, 16, 19));  // In your method: if (values.contains(x)) {     ... } 

A HashSet is used here to give good look-up performance - even very large hash sets are able to execute contains() extremely quickly.

If performance is not important, you can code the gist of it into one line:

if (Arrays.asList(12, 16, 19).contains(x)) 

but know that it will create a new ArrayList every time it executes.

like image 170
Bohemian Avatar answered Oct 06 '22 20:10

Bohemian


Do you want to switch to this??

switch(x) {     case 12:     case 16:     case 19:          //Do something         break;     default:         //Do nothing or something else..         break; } 
like image 25
Marsellus Wallace Avatar answered Oct 06 '22 22:10

Marsellus Wallace