Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8304329
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T17:49:01+00:00 2026-06-08T17:49:01+00:00

I am trying to verify that a variable is not equal to either 0

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T17:49:02+00:00Added an answer on June 8, 2026 at 5:49 pm

    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!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to verify whether an object is null or not, using this
I'm trying to verify that an Account Name is not already in use once
I'm trying to verify that certain fields in my form are numeric and not
I am trying to verify whether a variable that is passed can be converted
I'm trying to programmatically verify that a jar file has not been obviously tampered
I'm trying to create a UnitTest to verify that an object has been deleted.
I'm trying to get it to verify that it has the same item in
I'm trying to use mock to verify that an index property has been set.
While trying to verify to myself, that C# Equals for IEnumerables is a reference
I am trying to verify the content of a large list using Robotium, and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.