Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

if statement - Why non-equality check of one variable against many values always returns 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,

  • for C-like languages (where equality operators compare the actual string values; eg. , , )

    if (v != "x" || v != "y" || v != "z")
    {
        // the statements I want to be executed
        // if v is neither "x", nor "y", nor "z"
    }
    
  • for Pascal-like languages (e.g. )

    IF (v != 'x' OR v != 'y' OR v != 'z') THEN
        -- the statements I want to be executed
        -- if v is neither "x", nor "y", nor "z"
    END IF;
    

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

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

  • When v = "x",

    v != "x" becomes "x" != "x", which is false.
    v != "y" becomes "x" != "y", which is true.
    v != "z" becomes "x" != "z", which is true.

    The expression evaluates to false || true || true, which is true.

  • When v = "y", the expression becomes

      "y" != "x" || "y" != "y" || "y" != "z"
    

    or true || false || true, which is true.

  • When v = "z", the expression becomes

      "z" != "x" || "z" != "y" || "z" != "z"
    

    or true || true || false, which is true.

  • For any other value for v, the expression evaluates to true || true || true, which is true.

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,

  • for C-like languages (eg. , -(may need the strict equality operator !==), )

      if (v != "x" && v != "y" && v != "z")
      {
          // the statements I want to be executed
          // if v is neither "x", nor "y", nor "z"
      }
    
  • for Pascal-like languages

      IF (v != 'x' AND v != 'y' AND v != 'z') THEN
          -- the statements I want to be executed
          -- if v is neither "x", nor "y", nor "z"
      END IF;
    

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...