为什么一个变量对多个值的非等式检查总是返回true? [英] Why non-equality check of one variable against many values always returns true?

查看:213
本文介绍了为什么一个变量对多个值的非等式检查总是返回true?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序中有一个变量 v ,它可能会从值集中获取任何

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

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

我的目标是仅在 v 不是x时才执行某些语句y,或z

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

我试过,

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

if (v != "x" || v != "y" || v != "z")
{
    // the statements I want to be executed
    // if v is neither "x", nor "y", nor "z"
}


  • 对于Pascal类似语言(例如 plsql

    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;
    


  • if条件中的语句总是被执行。我做错了吗?

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

    推荐答案

    使用&& / AND / ,而非 || / /

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

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


    如果始终执行 if 块,则if块的条件总是计算为 true 。逻辑表达式必定是错误的。

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

    让我们考虑 v!=x|| v!=y|| v!=z每个 v 的值。


    • v =x时,

    v! =x变为x!=x,其中 false

    v!=y变为x!=y,其中给出的是 true

    v!=z变为x!=y,它给出的是 true

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

    表达式的计算结果为 false ||真|| true true

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


    v =y时,表达式变为

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

    true ||假|| true true


    v =z时,表达式变为

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

    true ||真|| false true


    对于 v 的任何其他值,表达式的计算结果为 true ||真|| true true

    For any other value for v, 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
    






    如您所见,您的逻辑表达式始终的计算结果为 true

    你想要做的是,找到一个计算结果为 true 的逻辑表达式

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


    (v不是x) ( v不是y) (v不是z)

    正确的结构是,

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

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


  • 用于Pascal类语言 plsql

    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定律,表达式也可以重写为(使用C语法) )

    By de Morgan's Law, the expression can also be rewritten as (using C syntax)

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

    含义


    ((v为x) (v为y) (v是z))

    这使得逻辑更加明显。

    某些语言具有用于测试集合中成员资格的特定构造,或者您可以使用数组/列表操作。

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


    • sql v NOT IN('x','y','z')

    javascript [x,y,z] .indexOf(v)== -1

    python v不在{x,y,z}

    这篇关于为什么一个变量对多个值的非等式检查总是返回true?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆