为什么一个variables对多个值的不等式检查总是返回true?

我在我的程序中有一个variablesv ,它可以从这组值中取任何

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

而我的目标是只有当v不是"x""y""z"时才执行一些语句。

我努力了,

  • 对于类C语言(等于运算符比较实际的string值,例如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条件中的语句总是被执行 。 我做错了什么?

使用&& / AND / and ,而不是|| / OR / or

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

如果总是执行一个if块,则if块的条件总是计算为true 。 逻辑expression式必然是错误的。

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

  • v = "x"

    v != "x"变成"x" != "x" ,这是错误的
    v != "y"变成"x" != "y" ,这是真实的
    v != "z"变成"x" != "y" ,这是真实的

    expression式评估为false || true || true false || true || true false || true || true ,这是真的

  • v = "y" ,expression式变成

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

    或者是true || false || true true || false || true true || false || true ,这是真的

  • v = "z" ,expression式变成

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

    或者是true || true || false true || true || false true || true || false ,这是真的

  • 对于v任何其他值,expression式评估为true || true || true true || true || true true || true || true ,这是真的

或者,考虑真值表:

  │ ABC │ 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 

正如你所看到的,你的逻辑expression式总是计算为true

你想要做的是,find一个逻辑expression式,计算结果为true

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

正确的build设是,

  • 对于类C语言(例如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' 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; 

通过德摩根定律 ,expression式也可以被重写为(使用C语法)

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

含义

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

这使得逻辑更加明显。


有些语言有特定的结构来testing集合中的成员资格,或者你可以使用数组/列表操作。

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

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

  • python : v not in {"x", "y", "z"}

你可以使用这样的东西,为PHP:

 if(strpos('xyz',$v[0])===false)//example 1 //strpos returns false when the letter isn't in the string //returns the position (0 based) of the substring //we must use a strict comparison to see if it isn't in the substring if(!in_array($v[0],array('x','y','z')))//example 2 //example 3 $out=array('x'=>1,'y'=>1,'z'=>1); //create an array if(!$out[$v[0]]) //check if it's not 1 if(!preg_match('/^[xyz]$/',$v))//example 4, using regex if(str_replace(array('x','y','z'),'',$v[0]))//example 5 if(trim($v[0],'xyz'))//example 6 

对于Javascript:

 if(~'xyz'.search(v[0]))//example 1(.indexOf() works too) if(!(v[0] in {x:0,y:0,z:0}))//example 2 if(~['x','y','z'].indexOf(v[0]))//example 3, incompatible with older browsers. if(!/^[xyz]$/.match(v))//example 4 if(v.replace(/^[xyz]$/))//example 5 

对于MySQL:

 Select not locate(@v,'xyz'); -- example 1 select @v not in ('x','y','z'); -- example 2 -- repetition of the same pattern for the others 

对于C:

 if(!strstr('xyz',v))//example 1, untested 

还有更多的方法,我只是太懒惰了。

用你的想象力,只写一个你喜欢的更多!