Search code examples
if-statementvariablesluacomparisonoperators

How to check if a value is equal or not equal to one of multiple values in Lua?


I am trying to verify that a variable is not equal to either 0 or 1. I tried using the following but neither work:

if x ~= (0 or 1) then
    print("x is not equal to 1 or 0")
end
if x ~= 0 or 1 then
    print("x is not equal to 1 or 0")
end

Conversely using == in this manner does not work either:

if x == (0 or 1) then
    print("x is equal to 1 or 0")
end
if x == 0 or 1 then
    print("x is equal to 1 or 0")
    return
end

Is there a way to do this?


Solution

  • Your problem stems from a misunderstanding of the or operator that is common to people learning programming languages like this. Yes, your immediate problem can be solved by writing x ~= 0 and x ~= 1, but I'll go into a little more detail about why your attempted solution doesn't work.

    When you read x ~= (0 or 1) or x ~= 0 or 1 it's natural to parse this as you would the sentence "x is not equal to zero or one". In the ordinary understanding of that statement, "x" is the subject, "is not equal to" is the predicate or verb phrase, and "zero or one" is the object, a set of possibilities joined by a conjunction. You apply the subject with the verb to each item in the set.

    However, Lua does not parse this based on the rules of English grammar, it parses it in binary comparisons of two elements based on its order of operations. Each operator has a precedence which determines the order in which it will be evaluated. or has a lower precedence than ~=, just as addition in mathematics has a lower precedence than multiplication. Everything has a lower precedence than parentheses.

    As a result, when evaluating x ~= (0 or 1), the interpreter will first compute 0 or 1 (because of the parentheses) and then x ~= the result of the first computation, and in the second example, it will compute x ~= 0 and then apply the result of that computation to or 1.

    The logical operator or "returns its first argument if this value is different from nil and false; otherwise, or returns its second argument". The relational operator ~= is the inverse of the equality operator ==; it returns true if its arguments are different types (x is a number, right?), and otherwise compares its arguments normally.

    Using these rules, x ~= (0 or 1) will decompose to x ~= 0 (after applying the or operator) and this will return 'true' if x is anything other than 0, including 1, which is undesirable. The other form, x ~= 0 or 1 will first evaluate x ~= 0 (which may return true or false, depending on the value of x). Then, it will decompose to one of false or 1 or true or 1. In the first case, the statement will return 1, and in the second case, the statement will return true. Because control structures in Lua only consider nil and false to be false, and anything else to be true, this will always enter the if statement, which is not what you want either.

    There is no way that you can use binary operators like those provided in programming languages to compare a single variable to a list of values. Instead, you need to compare the variable to each value one by one. There are a few ways to do this. The simplest way is to use De Morgan's laws to express the statement 'not one or zero' (which can't be evaluated with binary operators) as 'not one and not zero', which can trivially be written with binary operators:

    if x ~= 1 and x ~= 0 then
        print( "X must be equal to 1 or 0" )
        return
    end
    

    Alternatively, you can use a loop to check these values:

    local x_is_ok = false
    for i = 0,1 do 
        if x == i then
            x_is_ok = true
        end
    end
    if not x_is_ok then
        print( "X must be equal to 1 or 0" )
        return
    end
    

    Finally, you could use relational operators to check a range and then test that x was an integer in the range (you don't want 0.5, right?)

    if not (x >= 0 and x <= 1 and math.floor(x) == x) then
        print( "X must be equal to 1 or 0" )
        return
    end
    

    Note that I wrote x >= 0 and x <= 1. If you understood the above explanation, you should now be able to explain why I didn't write 0 <= x <= 1, and what this erroneous expression would return!