if-statementlanguage-agnosticlogical-operatorscontrol-flowdemorgans-law

Why does non-equality check of one variable against many values always return true?


I have a variable v in my program, and it may take any value from the set of values

"a", "b", "c", ..., "z"

And my goal is to execute some statement only when v is not "x", "y", or "z".

I have tried,

The statements inside the if condition always get executed. Am I doing anything wrong?


Solution

  • Use &&/AND/and, not ||/OR/or:

    v != "x" && v != "y" && v != "z"
    

    Problem

    If an if block is always executed, the condition for the if block always evaluates to true. The logical expression must be wrong.

    Let us consider v != "x" || v != "y" || v != "z" for each value of v.

    Alternatively, consider the truth-table:

           │     A          B          C      │
      v    │  v != "x"   v != "y"   v != "z"  │  A || B || C
    ───────┼──────────────────────────────────┼──────────────
     "x"   │    false      true       true    │     true
     "y"   │    true       false      true    │     true
     "z"   │    true       true       false   │     true
    other  │    true       true       true    │     true
    

    As you can see, your logical expression always evaluates to true.

    Solution

    What you want to do is, find a logical expression that evaluates to true when

    (v is not "x")and(v is not "y")and(v is not "z").

    The correct construction is,

    De Morgan's law

    By De Morgan's law, the expression can also be rewritten as (using C-like syntax)

    !(v == "x" || v == "y" || v == "z")
    

    meaning

    not((v is "x")or(v is "y")or(v is "z")).

    This makes the logic a bit more obvious.

    Specific languages

    Some languages have specific constructs for testing membership in sets, or you can use array/list operations.