• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

++b + b++ and b++ + ++b

 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Bijendra,
Your Quote,


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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thankyou..

regards,
William
 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
then what happens:
bb+ + + ++bb
it prints 3 if b=1
anyone can explain?
 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Bijendra S. Rajput:
I am getting the Javascript error.




Javascript?
 
Bijendra S. Rajput
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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: 1325
Android Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Muhammad Saifuddin
Ranch Hand
Posts: 1325
Android Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jothi,

Yeah you are right
but have to mension if something like that like compiling separately.. isn't it.
 
Muhammad Saifuddin
Ranch Hand
Posts: 1325
Android Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Friends,
please let me know will we get these sort of questions in SCJP 5.0 eal exam.
 
reply
    Bookmark Topic Watch Topic
  • New Topic