简体   繁体   English

这是对逻辑“和”运算符的正确使用吗?

[英]Is this a proper use of the logical “and” operator?

I'm looking at someone's code and they did: 我正在查看某人的代码,他们做到了:

if ( myNum > (num1 && num2 && num3) )

... with the intent of executing code when myNum is greater than num1, num2, and num3. ...旨在在myNum大于num1,num2和num3时执行代码。 Will this work? 这样行吗?

edit: Thanks guys. 编辑:谢谢大家。 I didn't think it would work, but I'm no expert, so I thought I'd ask people who are. 我认为这行不通,但是我不是专家,所以我想问问那些人。

Assuming the values are of a type like int , in C or C++ this would first evaluate 假设值是int类型的,在C或C ++中,它将首先求值

(num1 && num2 && num3)

as

(num1 !=0) && (num2 != 0) && (num3 != 0)

The result would be either true or false in C++, and either 1 or 0 in C. 结果在C ++中为truefalse在C中为1或0。

You would then compare myNum to that result. 然后,您将myNum与该结果进行比较。

No - certainly not in C# or Java, at least. 不-至少在C#或Java中肯定不会。 You want: 你要:

if ((myName > num1) && (myNum > num2) && (myNum > num3))

(It's possible that you don't need the brackets here - I can never remember operator precedence.) (有可能您在这里不需要括号-我永远不记得运算符的优先级。)

It wouldn't work as written in Java or C# as && is an operator on Boolean operands, with a Boolean result. 它不能用Java或C#编写,因为&&布尔操作数的运算符,并且具有布尔结果。 It's possible that it would compile/run in a less strongly-typed language, but I highly doubt that it will have the intended effect in any mainstream language. 它可能会以不太强类型的语言编译/运行,但是我高度怀疑它是否会在任何主流语言中产生预期的效果。

No, that won't work. 不,那行不通。 Why it won't work depends on the language, but (num1 && num2 && num3) will be evaluated first in all the languages you've tagged the question with. 为什么它不起作用取决于语言,但是(num1 && num2 && num3)将首先在您使用问题标记的所有语言中进行评估。 Provided (num1 && num2 && num3) actually compiles and evaluates to something, you will then get myNum > something. 假设(num1 && num2 && num3)实际上进行编译并求值,那么您将获得myNum> something。

Not in any language from the tags. 标签中没有任何语言。 Well, I think it might work in C and JavaScript, but certainly not as intended. 好吧,我认为它可能在C和JavaScript中工作,但肯定不是预期的那样。

It might work in c++ if myNum, num1, num2 and num3 are a special type with operators > and && overloaded. 如果myNum,num1,num2和num3是带有>和&&重载的特殊类型,则它可能在c ++中工作。 But it will probably not worth it creating such a class which can handle this. 但是创建一个可以处理此问题的类可能不值得。

This is incorrect at least in C: http://codepad.org/R9IP0ZOr 至少在C语言中这是错误的: http : //codepad.org/R9IP0ZOr

int main()
{

  int myNum = 10;
  int num1,num2,num3 ;
  num1 = 15;
  num2 = 16;
  num3 = 17;
  if ( myNum > (num1 && num2 && num3) )
      printf("%s","YES|GREATER");
  else
    printf("%s","NO|SMALLER");
}   // prints "YES|GREATER"  even though all numbers are greater than myNum

Correct statement should be : 正确的陈述应为:

if(  (myName > num1) && (myNum > num2) && (myNum > num3) )
{
}

It is valid JavaScript code, but will probably work differently than expected by the person who wrote it. 它是有效的JavaScript代码,但可能会与编写它的人所预期的工作方式不同。

It will actually end up evaluating if(myNum > num3) since (num1 && num2 && num3) will just return the value of num3 它实际上将最终评估if(myNum > num3)因为(num1 && num2 && num3)将仅返回num3的值

或更幻想:

if(Num > Math.Max(Num1,Math.Max(Num2,Num3)))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM