Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I need help making an if statement with many logical operators easier

Tags:

java

I've tried searching for it, but I don't really now how to formulate the question correctly...

I have an if-statement with many logical operators. How do I do this easier ?

If (n == 1 ||n == 2 ||n == 3  ||n == 5 ||n == 9 ||n == 8 ||n == 7 ||n == 551 ||n == 17 ||n == 81 || etc etc)
{ //Stuff
}

I'm thinking in pseudo-code I want something like this:

List list = {1, 3, 5, 7, 9, 12, 14, 16, 18, 19, 21, 23, 25, 27, 30, 32, 34, or 36 }

if n is in list, then {}

As you understand, I'm a beginner and I have problems formulating what I need.

like image 622
tore Avatar asked Apr 26 '13 13:04

tore


People also ask

How do you use logical operators in if statements?

With if statements we often use the following logical operators: The logical AND operator ( && ) only returns true when the expression on its left and the one on its right are both true too. When the left, right, or both values are false , then && returns false too.

How do you do multiple logical tests in Excel?

If you want to evaluate multiple logical tests within a single formula, then you can nest several functions one into another. Such functions are called nested IF functions. They prove particularly useful when you wish to return different values depending on the logical tests' results.

How do you write an IF THEN statement in Excel with multiple conditions?

Type =IF( Excel will display the logical hint just below the cell F2. The parameters of this function are logical_test, value_if_true, value_if_false. The first parameter contains the condition to be matched. You can use multiple If and AND conditions combined in this logical test.

How many && operators can be used in one if statement?

There is no limit to the number of && you use in a statement. So it will work with 4, 5, 100. It fails because some of the conditions are falsey.


5 Answers

How about:

int[] validOptions = { 1, 2, 3, 5, 7, 8, 9, 17, 81, 551 };

if (Arrays.binarySearch(validOptions, n) >= 0)
{
    // Yup, found it
}

Note that I reordered your original check (which had 551 before 17 for example) so that the array is sorted. A binary search will only work with sorted data.

I'm only suggesting an array here on the grounds that it's slightly easier to specify, particularly as you're dealing with a primitive type. If you want these to be dynamic, a List<Integer> or a HashSet<Integer> would be more appropriate.

Note that while it almost certainly doesn't matter while the set is small, it's worth considering the performance characteristics of different lookups:

  • HashSet.contains - O(1)
  • Arrays.binarySearch - O(log N)
  • ArrayList.contains - O(N)
like image 111
Jon Skeet Avatar answered Oct 01 '22 23:10

Jon Skeet


Yes, throw everything in an ArrayList and then use the .contains(Object obj) method:

List<Integer> numbers = new ArrayList<Integer>();
number.add(...;

if(numbers.contains(n))
{
    ...
}
like image 38
npinti Avatar answered Oct 02 '22 00:10

npinti


Try this:

 static private Set<Integer> set = new HashSet<Integer>(
     Arrays.asList(1, 3, 5, 7, 9, 12, 14, 16, 18, 19, 21, 23, 25, 27, 30, 32, 34, 36));

then to test:

if (set.contains(n))

Using a HashSet will make it perform pretty well.

like image 35
Bohemian Avatar answered Oct 01 '22 23:10

Bohemian


Your List approach is correct. You can do something like this:

int[] values = {1, 3, 5, 7, 9, 12, 14, 16, 18, 19, 21, 23, 25, 27, 30, 32, 34, 36 };
List<Integer> list = new ArrayList<Integer>();
if(values.asList(list).contains(n)) {
   ...
}
like image 27
Aleks G Avatar answered Oct 01 '22 23:10

Aleks G


function arrayinside(n, listofn) {
    var length = listofn.length;
    for(var i = 0; i < length; i++) {
        if(listofn[i] == n) return true;
    }
    return false;
}

Try this

like image 37
premananth Avatar answered Oct 01 '22 23:10

premananth