为什么一个变量对多个值的非等式检查总是返回true? [英] Why non-equality check of one variable against many values always returns 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"
.
我试过,
-
用于类C语言(等式运算符比较实际字符串值;例如, c#, javascript , php )
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)
。
正确的结构是,
-
用于类C语言(例如 c#, javascript - (可能需要严格的相等运算符
!==
), php )
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屋!