Question

How can I check if a condition passes multiple values?

Example:

if(number == 1,2,3)

I know that commas don't work.

Was it helpful?

Solution

if (number == 1 || number == 2 || number == 3)

OTHER TIPS

If you are using PHP, then suppose your list of numbers is an array

$list = array(1,3,5,7,9);

then for any element, you can use

if(in_array($element, $list)){
//Element present in list
}else{
//not present.
}

Function structure:

bool in_array ( mixed $needle , array $haystack [, bool $strict = FALSE ] )

Hope that helps.

if ((number >= 1) && (number <= 3))

What language?

For example in VB.NET you use the word OR, and in C# you use ||

Since you specify no language I add a Python solution:

if number in [1, 2, 3]:
    pass

In T-SQL you can use the IN operator:

select * from MyTable where ID in (1,2,3)

If you are using a collection there may be a contains operator for another way to do this.

In C# for another way that may be easier to add values:

    List<int> numbers = new List<int>(){1,2,3};
    if (numbers.Contains(number))

I'll assume a C-Style language, here's a quick primer on IF AND OR logic:

if(variable == value){
    //does something if variable is equal to value
}

if(!variable == value){
    //does something if variable is NOT equal to value
}

if(variable1 == value1 && variable2 == value2){
    //does something if variable1 is equal to value1 AND variable2 is equal to value2
}

if(variable1 == value1 || variable2 = value2){
    //does something if variable1 is equal to value1 OR  variable2 is equal to value2
}

if((variable1 == value1 && variable2 = value2) || variable3 == value3){
    //does something if:
    // variable1 is equal to value1 AND variable2 is equal to value2
    // OR variable3 equals value3 (regardless of variable1 and variable2 values)
}

if(!(variable1 == value1 && variable2 = value2) || variable3 == value3){
    //does something if:
    // variable1 is NOT equal to value1 AND variable2 is NOT equal to value2
    // OR variable3 equals value3 (regardless of variable1 and variable2 values)
}

So you can see how you can chain these checks together to create some pretty complex logic.

For a list of integers:

static bool Found(List<int> arr, int val)
    {
        int result = default(int);
        if (result == val)
            result++;

        result = arr.FindIndex(delegate(int myVal)
        {
            return (myVal == val);
        });
        return (result > -1);
    }

In Java you have objects that wrap primitive variables (Integer for int, Long for long, etc). if you look to compare values between a lot of complete numbers (ints), what you can do is initiate a bunch of Integer objects, stuff them inside an iterable such as an ArrayList, iterate over them and compare.

something like:

ArrayList<Integer> integers = new ArrayList<>();
integers.add(13);
integers.add(14);
integers.add(15);
integers.add(16);

int compareTo = 17;
boolean flag = false;
for (Integer in: integers) {
    if (compareTo==in) {
    // do stuff
    }
}

of course for a few values this may be a bit unwieldy, but if you want to compare against a lot of values, it'll work nicely.

Another option is to use java Sets, you can place a lot of different values (the collection will sort your input, which is a plus) and then invoke the .contains(Object) method to locate equality.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top