为什么有四个逻辑运算符:
&, &&
|, ||用法有什么不同?
是的,我已经检查过文档了,但我还是有点困惑。文件上写着:
‘&’ and ‘&&’ indicate logical AND and ‘|’ and ‘||’ indicate
logical OR. The shorter form performs elementwise comparisons in
much the same way as arithmetic operators. The longer form
evaluates left to right examining only the first element of each
vector. Evaluation proceeds only until the result is determined.
The longer form is appropriate for programming control-flow and
typically preferred in ‘if’ clauses.我认为一个例子可以清楚地展示它们。谢谢。
发布于 2013-04-16 10:40:20
主要区别如下:
False。因此,它返回false。在执行||时,如果前几个元素中的比较结果为真,我们可以肯定地说,任何进一步的验证都不会更改结果,因此它将返回True。希望这能有所帮助。
& and &&表示逻辑AND and | and ||表示逻辑OR。较短的形式执行元素比较的方式与算术运算符非常相似。较长的形式从左到右计算,只检查每个向量的第一个元素。只有在结果确定之前,评估才会进行。较长的形式适用于控制流编程,并且通常在if子句中首选。
来源:http://stat.ethz.ch/R-manual/R-patched/library/base/html/Logic.html
https://stackoverflow.com/questions/16027840
复制相似问题