逻辑和 [英] Logical And

查看:54
本文介绍了逻辑和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请查看下面的代码

#include< stdio.h>


int expr(char str [],int i){

printf("%s \ n",str);

返回i;

}


int main()

{

if(expr(" 1st",1)|| expr(" 2nd",0)&& expr(3rd,1));

返回0;

}


输出

-------

1st


As&&优先于||

然后它应该调用expr(2nd,0)或expr(3rd,0)

首先为什么呢电话(expr(1st,1)首先


问候

Shiju

解决方案

sh**********@hotmail.com (山姆)写在

新闻:25 ************************** @ posting.google.c om:

请查看下面的代码

#include< stdio.h>

int expr(char str [],int i ){
printf("%s \ n",str);
返回i;
}
int main()
{
if(expr(" 1st",1)|| expr(" 2nd",0)&&& expr(" 3rd",1));
返回0;
}

输出
-------
第一个

As&&&&&&&&&& >然后它应该调用expr(2nd,0)或expr(3rd,0)
首先为什么它调用(expr(1st,1)first




它被称为短路。如果expr(1st,1)为真,则没有

需要打扰evaluting expr(3rd,0)然后expr(2nd,0),所以它确实没有b $ b。你所看到的是正确的。


-

- 马克 - >

-


sam< sh ********** @ hotmail.com>这样说:

int main()
if(expr(" 1st",1)|| expr(" 2nd",0) && expr(" 3rd",1));
返回0;
}
As&&优先级高于||
然后它应该调用expr(2nd,0)或expr(3rd,0)
然后首先调用它(expr(1st) ,1)第一次




这有意义吗?


*> +

A + B * C等于A +(B * C)

&&> ||

A || B&& C等于A ||(B& C)


正如马克所说,最后一个表达式需要进行短路

评估,因此有无需评估B& C(因为在这种情况下A是

为真)。请注意


B&& C ||一个


在这种情况下叫#2和#1,但不是#3,再次因为

的短路评估。


-

克里斯托弗·本森 - 马尼卡|我应该*知道我在说什么 - 如果我在网络空间获得报酬。 org |不,我需要知道。欢迎火焰。


sam写道:
请看下面的代码

#include< stdio.h>

int expr(char str [],int i){
printf("%s \ n",str);
返回i;
}
int main()
{
if(expr(1st,1)|| expr(2nd,0)&& expr(3rd,1));
返回0;
}
输出
-------
第一

As&&优先级高于||
然后它应该调用expr(2nd,0)或expr(3rd,0)
然后首先调用它(expr(1st) ,1)首先




运算符优先级和评估顺序是两个不同的东西。优先权决定了< br $>
表达意味着


expr(" 1st",1)||(expr(" 2nd",0)&& expr(""第3个,0)


而不是


(expr(1st,1)|| expr("2nd) ,0))&& expr(3rd,0)


....但仅优先权并不能确定
$ b中的顺序三个expr()调用的$ b。


评估顺序不是由优先级确定的,而是由b |的定义决定的。在这个

的情况下,||的规则说如果expr(1st,1)产生一个

非零va lue,第二个子表达式没有被评估

。如果expr(1st,1)产生零,则第二个子

表达式*被评估 - 并且在该评估中,那里

是类似的规则和&&管理

expr(2nd,0)和expr(3rd,0)的评估顺序。


摘要:运算符优先级控制具有多个运算符的

表达式的含义,但不控制操作数的计数顺序。


-
Er*********@sun.com


Please look at the code below
#include <stdio.h>

int expr(char str[], int i){
printf("%s \n",str);
return i;
}

int main()
{
if(expr("1st",1) || expr("2nd",0) && expr("3rd",1));
return 0;
}

output
-------
1st

As && has an higher precedence over ||
then it should call expr("2nd",0) or expr("3rd",0)
first then why it calls (expr("1st",1) first

Regards
Shiju

解决方案

sh**********@hotmail.com (sam) wrote in
news:25**************************@posting.google.c om:

Please look at the code below
#include <stdio.h>

int expr(char str[], int i){
printf("%s \n",str);
return i;
}

int main()
{
if(expr("1st",1) || expr("2nd",0) && expr("3rd",1));
return 0;
}

output
-------
1st

As && has an higher precedence over ||
then it should call expr("2nd",0) or expr("3rd",0)
first then why it calls (expr("1st",1) first



It''s called a short-circuit. If expr("1st",1) is true, then there is no
need to bother evaluting expr("3rd",0) and then expr("2nd",0), so it does
not. What you have seen is correct.

--
- Mark ->
--


sam <sh**********@hotmail.com> spoke thus:

int main()
{
if(expr("1st",1) || expr("2nd",0) && expr("3rd",1));
return 0;
} As && has an higher precedence over ||
then it should call expr("2nd",0) or expr("3rd",0)
first then why it calls (expr("1st",1) first



Does this make sense?

* > +
A + B * C equals A + (B * C)

&& > ||
A || B && C equals A || (B && C)

As Mark stated, the last expression is subject to short-circuit
evaluation, and thus there is no need to evaluate B && C (since A is
true in this case). Notice that

B && C || A

in this case calls #2 and #1, but not #3, again because of
short-circuit evaluation.

--
Christopher Benson-Manica | I *should* know what I''m talking about - if I
ataru(at)cyberspace.org | don''t, I need to know. Flames welcome.


sam wrote:


Please look at the code below

#include <stdio.h>

int expr(char str[], int i){
printf("%s \n",str);
return i;
}

int main()
{
if(expr("1st",1) || expr("2nd",0) && expr("3rd",1));
return 0;
}

output
-------
1st

As && has an higher precedence over ||
then it should call expr("2nd",0) or expr("3rd",0)
first then why it calls (expr("1st",1) first



"Operator precedence" and "order of evaluation" are
two different things. Precedence dictates that the
expression means

expr("1st",1) || ( expr("2nd",0) && expr("3rd",0) )

rather than

( expr("1st",1) || expr("2nd",0) ) && expr("3rd",0)

.... but precedence alone doesn''t determine the order in
which the three expr() calls are made.

The evaluation order is determined not by the precedence,
but by the definitions of the || and && operators. In this
case, the rule for || says that if expr("1st",1) produces a
non-zero value, the second sub-expression is not evaluated
at all. If the expr("1st",1) yields zero, the second sub-
expression *is* evaluated -- and in that evaluation, there
is a similar rule for && that governs the order in which
expr("2nd",0) and expr("3rd",0) are evaluated.

Summary: Operator precedence governs the meaning of an
expression with multiple operators, but does not control
the order in which the operands are evaluated.

--
Er*********@sun.com


这篇关于逻辑和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆