• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

DEEP trouble in post/prefix notations

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pl. help me how to solve the following codes :
Int x=1;
Ln1 :System.out.println((++x)+(x++)+(x--)+(--x));
Ln2 :System.out.println(x);
My reasoning :
I took first bracket prefix notation, & incremented
The x vaue by 1, the new value of x becomes 2.
Now I took the last bracket prefix notation decremented
The x's present value(ie 2) by 1.The new value of x
Becomes 1.than I added like :
System.out.println ((2)+(1)+(1)+(1)); // 5
. . . I know iam not calculating it in right manner.
Is there anyone who could tell me the best method to calculate
Such interesting exp.
The output for the above is :
O/p Ln1 : 8
O/p Ln2 : 1
Thax,
Vikas S.
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Evaluate your expression from left to right and you will get your answer !
 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi vikas
first thing put it as int x and not Int x
coming to yr question
start x = 1
++x = 2 so x becomes 2
x++ = 2 but after this operation x becomes 3
x-- = 3 but after this operation x becomes 2
--x = 1 so x becomes 1
adds up to : 2+2+3+1 = 8
and x = 1.
 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Vikas,
Follow this, as someone explained it to me.
put the operator values in the bracket and operand values outside than drop the values from the bracket, try this you never go wrong.
Int x=1;
Ln1 :System.out.println((++x)+(x++)+(x--)+(--x));
(2)2 + 2(3) + 3(2) + (1)1
Now drop the values from the bracket,
2 + 2 + 3 +1 = 8
Ln2 :System.out.println(x);
the last value of i was '1'
Hope it helps...
 
vikas singh
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
dear Wasim,
the formula was great . . .
Thanx,
Vikas.
 
I AM MIGHTY! Especially when I hold this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic