理解++ [英] Understanding ++
问题描述
有人可以帮我理解这个:
{
int i = -3,j = 2,k = 0,m;
m = ++ i&& ++ j || ++ k;
}
{
int i = -3,j = 2,k = 0,m;
m = ++ i || ++ j&& ++ k;
}
两种情况下变量的最终值是多少?
怎么样?
TIA。
ye*@yes.yes 这样说:
{
int i = -3,j = 2,k = 0,m;
m = ++ i&& ++ j || ++ k;
}
&&优先于||,所以这相当于
m =(++ i&& ++ j)|| ++ k;
假设你没有忘记循环或某事,你得到
m =( - 2&& 3)|| 1;
所以m = 1(真),i = -2,j = 3,k = 1.
{
int i = -3,j = 2,k = 0,m;
m = ++ i || ++ j&& ++ k;
}
m = -2 || (3&& 1);
给你相同的结果。
现在,如果你的意思是
while(++ i& ++ j || ++ k);
你得到
(-2&& 3)|| 1
(-1&& 4)|| 2
(0&& 5)|| 3
等
这是一个无限循环。
while(++ i || ++ j& ++ k);
你得到
- 2 || ......< - 短路评估
-1 || ......
0 || (3&& 1)
1 || ...
..等等
另一个无限循环。请注意,如果k为-1而不是0,则最终得到
-2 || ......
-1 || ......
0 ||| (3&& 0)< - false
所以i = 0,j = 3,k = 0。 (我已经测试了这个^ _ ^)
如果我搞砸了某个地方,有人请指正! (并且抱歉做了什么似乎是家庭作业的问题 - 我刚刚意识到这一点,该死的如果我写完所有这些之后我不会发布
帖子... )
-
Christopher Benson-Manica |在你的命运转向轮子,
ataru(at)cyberspace.org |在你的课上学习。
Christopher Benson-Manica写道:ye*@yes.yes 这样说:
{
int i = -3,j = 2,k = 0,m ;
m = ++ i&& ++ j || ++ k;
}
&&优先于||,所以这相当于
m =(++ i&& ++ j)|| ++ k;
给Nitpick,&&和||运营商具有相同的优先权。
&&首先评估运算符,因为运算符从左到右进行评估(因为它们具有相同的优先级b / b
)。
-
Thomas Matthews
C ++新闻组欢迎辞:
http://www.slack.net/~shiva/welcome.txt
C ++常见问题: http://www.parashift.com/c++-faq-lite
C常见问题: http:// www。 eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c ++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
其他网站:
http://www.josuttis .com - C ++ STL图书馆书
2003年10月1日星期三20:51:59 +0000(UTC),在comp.lang.c中你
写道:
ye *@yes.yes因此说:
{
int i = -3,j = 2,k = 0,m;
m = ++ i&& ++ j || ++ k;
}
&&优先于||,所以这相当于
m =(++ i&& ++ j)|| ++ k;
假设你没有忘记循环或某事,你得到
m =( - 2&& 3)|| 1;
#include< stdio.h>
int main(无效)
{
int i = -3,j = 2,k = 0,m;
m = ++ i&& ++ j || ++ k;
printf("%d%d%d%d",i,j,k,m);
返回0;
}
所以m = 1(真),i = -2,j = 3,k = 1.
i,j, k,m
-2 3 0 1
Could someone help me understand this:
{
int i=-3,j=2,k=0,m;
m=++i && ++j || ++k;
}
{
int i=-3,j=2,k=0,m;
m=++i || ++j && ++k;
}
What are the final values of variable in both cases, and
how?
TIA.
解决方案ye*@yes.yes spoke thus:
{
int i=-3,j=2,k=0,m;
m=++i && ++j || ++k;
}
&& has precedence over ||, so this is equivalent to
m=(++i && ++j) || ++k;
Assuming you didn''t forget a loop or something, you get
m=(-2 && 3) || 1;
so m=1 (true), i=-2, j=3, and k=1.
{
int i=-3,j=2,k=0,m;
m=++i || ++j && ++k;
}
m=-2 || (3 && 1);
which gives you the same result.
Now, if you meant something like
while( ++i && ++j || ++k );
you get
( -2 && 3 ) || 1
( -1 && 4 ) || 2
( 0 && 5 ) || 3
etc.
This is an infinite loop.
For
while( ++i || ++j && ++k );
you get
-2 || ... <- short circuit evaluation
-1 || ...
0 || ( 3 && 1 )
1 || ...
..etc
Another infinite loop. Notice that if k is -1 instead of 0, you end up with
-2 || ...
-1 || ...
0 ||| ( 3 && 0 ) <- false
so i=0, j=3, and k=0. (I have tested this ^_^)
If I screwed up somewhere, someone please correct me! (And sorry for doing
what seems like a homework problem - I just realized it, and damn if I won''t
post after I wrote all this...)
--
Christopher Benson-Manica | Upon the wheel thy fate doth turn,
ataru(at)cyberspace.org | upon the rack thy lesson learn.
Christopher Benson-Manica wrote:ye*@yes.yes spoke thus:{
int i=-3,j=2,k=0,m;
m=++i && ++j || ++k;
}
&& has precedence over ||, so this is equivalent to
m=(++i && ++j) || ++k;
To Nitpick, the && and || operators have equal precedence.
The && operator is evaluated first because the operators
are evaluated left to right (since they have equal
precedence).
--
Thomas Matthews
C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
On Wed, 1 Oct 2003 20:51:59 +0000 (UTC), in comp.lang.c you
wrote:
ye*@yes.yes spoke thus:{
int i=-3,j=2,k=0,m;
m=++i && ++j || ++k;
}
&& has precedence over ||, so this is equivalent to
m=(++i && ++j) || ++k;
Assuming you didn''t forget a loop or something, you get
m=(-2 && 3) || 1;
#include <stdio.h>
int main(void)
{
int i=-3,j=2,k=0,m;
m=++i && ++j || ++k;
printf("%d %d %d %d",i,j,k,m);
return 0;
}
so m=1 (true), i=-2, j=3, and k=1.
i, j, k, m
-2 3 0 1
这篇关于理解++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!