为什么针对多个值对一个变量进行非等式检查总是返回 true? [英] Why does non-equality check of one variable against many values always return 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; e.g. 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
/and
,而不是 <代码>||/OR
/or
:
Use &&
/AND
/and
, not ||
/OR
/or
:
v != "x" && v != "y" && v != "z"
问题
如果 if
块总是被执行,则 if 块的条件 总是评估为 true
.逻辑表达式一定是错误的.
Problem
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
的每个值.
Let us consider v != "x" || v != "y" || v != "z"
for each value of v
.
当
v = "x"
时,
v != "x"
变成 "x";!= "x"
,这是 false.v != y"
变成 x";!= "y"
,这是 true.v != "z"
变成 "x";!= "z"
,这是真.
v != "x"
becomes "x" != "x"
, which is false.
v != "y"
becomes "x" != "y"
, which is true.
v != "z"
becomes "x" != "z"
, which is true.
表达式计算结果为 false ||真||真
,即真.
The expression evaluates to false || true || true
, which is true.
当v = "y"
时,表达式变为
"y" != "x" || "y" != "y" || "y" != "z"
或 true ||假||真
,即真.
当v = "z"
时,表达式变为
"z" != "x" || "z" != "y" || "z" != "z"
或 true ||真||假
,这是真.
对于 v
的任何其他值,表达式的计算结果为 true ||真||真
,即真.
For any other value for v
, the expression evaluates to true || true || true
, which is true.
或者,考虑真值表:
│ 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
.
As you can see, your logical expression always evaluates to true
.
你想要做的是,找到一个逻辑表达式,当
What you want to do is, find a logical expression that evaluates to true
when
(v 不是x")
and
(v 不是y")
and
(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;
根据德摩根定律,该表达式也可以改写为(使用 C-像语法)
By De Morgan's law, the expression can also be rewritten as (using C-like syntax)
!(v == "x" || v == "y" || v == "z")
意义
not
((v 是x")
or
<代码>(v 是y")or
(v 是z"))
.
这使得逻辑更明显一些.
This makes the logic a bit more obvious.
某些语言具有用于测试集合成员资格的特定构造,或者您可以使用数组/列表操作.
Some languages have specific constructs for testing membership in sets, or you can use array/list operations.
sql:
v 不在 ('x', 'y', 'z')
javascript: [x",y",z"].indexOf(v) == -1
python: v 不在 {x"、y"、z"}
java:!Arrays.asList("x", "y", "z").contains(v)
java-9(及以上):!Set.of("x", "y", "z").contains(v)
这篇关于为什么针对多个值对一个变量进行非等式检查总是返回 true?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!