This week's book giveaway is in the Design forum.We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!

# ++b + b++ and b++ + ++b

William Yan
Ranch Hand
Posts: 69
Hi everyone,
i cannot understand how ++b + b++ and b++ + ++b work, who'd drop me a light? more details are prefered. Thank you, ranchers.

Regards,
William

Joe Harry
Ranch Hand
Posts: 10106
3
Hi William,

Say for example int b = 1;
Now ++b + b++ is eveluated as (++b) + (b++) = (2) + (2) = 4
and b++ + ++b is evaluated as (b++) + (++b) = (1) + (3) = 4

Note that post fix and prefix operators have higher precedence than + operator and they associate from left to right.

Regards,
Jothi Shankar Kumar. S

Bijendra S. Rajput
Ranch Hand
Posts: 41
Hi,

then for first one, if b=1..
++b+b++
(++b)+(b++)
1+1
2

and for second one, if b=1
b+++++b
b+++(++b)
1+++(2)
1+(++2)
1+3
4

Bijendra R.

Joe Harry
Ranch Hand
Posts: 10106
3
Hi Bijendra,

then for first one, if b=1..
++b+b++
(++b)+(b++)
1+1
2

It will print 4. Run that and check for yourself.

Regards,
Jothi Shankar Kumar. S
[ October 18, 2006: Message edited by: Jothi Shankar Kumar Sankararaj ]

Bijendra S. Rajput
Ranch Hand
Posts: 41
sorry i did wrong by mistake....

for first one, if b=1..
++b+b++
(++b)+(b++)
2+2
4

and for second one, if b=1
b+++++b
b+++(++b)
1+++(2)
1+(++2)
1+3
4

And one more thing...for assignment operator. Before assigning prefix is done and after assigning post fix is done.

Regards,

Bijendra R.

William Yan
Ranch Hand
Posts: 69
Thankyou..

regards,
William

James Quinton
Ranch Hand
Posts: 94
then what happens:
bb+ + + ++bb
it prints 3 if b=1
anyone can explain?

Joe Harry
Ranch Hand
Posts: 10106
3
Hi James,

bb+ + + ++bb //here for the bb++, you are not incrementing it. Watch closely it is bb+ and not bb++. Hence the result 3.

Regards,
Jothi Shankar Kumar. S

James Quinton
Ranch Hand
Posts: 94
Originally posted by Jothi Shankar Kumar Sankararaj:
Hi James,

bb+ + + ++bb //here for the bb++, you are not incrementing it. Watch closely it is bb+ and not bb++. Hence the result 3.

Regards,
Jothi Shankar Kumar. S

I understand there is a space between first and second +. This spaces does matter.
but why does it result in 3? What's the meaning of three + signs here?
if a=1, i can do:
a+ + + + + + + +a
it still 2, same as a+a.
how come does compiler allows this?

Bijendra S. Rajput
Ranch Hand
Posts: 41
Hi James and Jothi,

Line b++ + ++b // printing 4

Line b+ + + + +b // printing 2

Line b+ + + ++b // printing 3

closely watch the spaces.

and when I am writing bb+, I am getting the Javascript error. How do not know how you guys are comiling.

Regards,

Bijendra R.

Barry Gaunt
Ranch Hand
Posts: 7729
Originally posted by Bijendra S. Rajput:
I am getting the Javascript error.

Javascript?

Bijendra S. Rajput
Ranch Hand
Posts: 41
Hi Barry

Javascript?

sorry...sorry.....

Actually I have a junk of coding in javascript, so by mistake i wrote.

I mean, I got compile error.

Please explain this and focus some light on a + + +a how compiler is compiling this.

Thanks,

Regards,
Bijendra R.

Joe Harry
Ranch Hand
Posts: 10106
3
Hi Above,

Ok, now I'm also interested if anyone can shed some light on this.
a + + +a how compiler is compiling this

I was tracing this back to basic mathematics where we perform operations like this,

a- -b - -c + -d >> a - (-b) - (-c) + (-d) >> a+b+c-d

So does the compiler also work the same way? Anyone please explain on this.

Regards,
Jothi Shankar Kumar. S

Ranch Hand
Posts: 1324
Originally posted by Bijendra S. Rajput:
Hi James and Jothi,

Line b++ + ++b // printing 4
Line b+ + + + +b // printing 2
Line b+ + + ++b // printing 3

but my result is different..

b = 1;
++b + b++; //printing 4
b+ + + + +b; //priting 6
b+ + + ++b; //printing 7

by which value you initialize b variable..?

Joe Harry
Ranch Hand
Posts: 10106
3
Hi Saif,

Originally posted by Bijendra S. Rajput:
Hi James and Jothi,

Line b++ + ++b // printing 4
Line b+ + + + +b // printing 2
Line b+ + + ++b // printing 3

The above code meant that each and every line is compiles seperatly with b=1.

Regards,
Jothi Shankar Kumar. S

Ranch Hand
Posts: 1324
Thanks Jothi,

Yeah you are right
but have to mension if something like that like compiling separately.. isn't it.

Ranch Hand
Posts: 1324
Originally posted by Bijendra S. Rajput:

when I am writing bb+

after read this "bb+" compiler start searching to find variable with name of bb if nothing, then it come up with the error : cannot resolve symbol.
[ October 18, 2006: Message edited by: Saif Uddin ]

d jones
Ranch Hand
Posts: 76
Hi,

b=1;
b++ + ++b //This evaluates to 4

However,
b + + ++b // This evaluates to 3

Can anyone please explain to me why the second expression evaluates to 3?
The only thing that is different is that I have removed the post increment operator on the first b, which shouldn't affect the value of the expression anyway since it is a post increment operator.

Thanks

Sandeep Vaid
Ranch Hand
Posts: 392
Friends,
please let me know will we get these sort of questions in SCJP 5.0 eal exam.