Understanding ++

Y

yes

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.
 
C

Christopher Benson-Manica

(e-mail address removed) 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...)
 
T

Thomas Matthews

Christopher said:
(e-mail address removed) spoke thus:




&& 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
 
M

me

(e-mail address removed) spoke thus:


&& 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
 
C

Christopher Benson-Manica

&& 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;

Even though someone else *already* corrected me, I want to come forward and
admit that I am dumb... it's funny how I only remembered short-circuit
evaluation halfway through this post. Of course this is

m=(-2 && 3) ... -> i=-2,j=3,k=0
m=-2 || ... -> i=-2,j=2,k=0

One day I'll make a totally correct post - until then I'll just have to
pretend I look sexy in this dunce cap...
 
C

Christopher Benson-Manica

Thomas Matthews said:
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).

According to a book I've respected until now, && is just higher than ||. If
the Standard says differently, I may have to revise my opinion of said book.
 
Y

you

Even though someone else *already* corrected me, I want to come forward and
admit that I am dumb...

Not at all. Everyone makes mistakes.
it's funny how I only remembered short-circuit
evaluation halfway through this post. Of course this is

m=(-2 && 3) ... -> i=-2,j=3,k=0
m=-2 || ... -> i=-2,j=2,k=0

I don't think the logic is clear. Could you (or someone
else) explain it in more detail?

Thanks.

Also note:
#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;
}

-2 2 0 1
 
R

Robert Stankowic

Even though someone else *already* corrected me, I want to come forward and
admit that I am dumb...

Not at all. Everyone makes mistakes.
it's funny how I only remembered short-circuit
evaluation halfway through this post. Of course this is

m=(-2 && 3) ... -> i=-2,j=3,k=0
m=-2 || ... -> i=-2,j=2,k=0

I don't think the logic is clear. Could you (or someone
else) explain it in more detail?

Thanks.

Also note:
#include <stdio.h>
int main(void)
{
int i=-3,j=2,k=0,m;
m=++i || ++j && ++k;

++i yiels -2.
-2 != 0 therefore the whole exprssion yields "true" which is by definition 1
due to lazy evaluation[1] the rest of the expression is skipped and we have
m == 1 (the standars´d value a true logical expression yields)
i == -2 (this part of the expression was evaluated)
j == 2
k == 0
because ++j as well as ++k were never executed

printf("%d %d %d %d",i,j,k,m);
return 0;
}

-2 2 0 1

[1] "lazy evaluation means, that the parts of a logical expression are only
evaluated until the outcome is known:
in
a || b for example b is only evaluated if a yields false
in
a && b b is only evaluated if a yields true

HTH
Robert
 
P

pete

Thomas said:
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).

Whta you just wrote, makes it seem as though you believe that
(1 || 1 && 0) is equivalent to ((1 || 1) && 0)
That can't be what you mean, can it ?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,083
Messages
2,570,591
Members
47,212
Latest member
RobynWiley

Latest Threads

Top