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

Result of this (a++ + ++a);

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
can anybody tell about this
int a=10;
for(int i=0,i<4,i++)
System.out.println(a++ + ++a);
why v get that ans's
22
26
30
34
thanx
aftab
 
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is why:
You have a memory location a which holds an int.
A side-effect of both expressions a++ and ++a is that the value of a is incremented.
However, the value of expression a++ is the value a had before increment, whereas the value of ++a is the value it has after increment.
So, in the first example where a is originally 10, we have:
* a++ evaluates to 10, but increments a to 11
* subsequent +a evaluated to 11+1 = 12
In total 10 + 12 = 22
Similar for loops i =1,2,3.
Hope this helps,
Panagiotis.

Originally posted by Aftab Ahmed:
Hi,
can anybody tell about this
int a=10;
for(int i=0,i<4,i++)
System.out.println(a++ + ++a);
why v get that ans's
22
26
30
34
thanx
aftab


 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Here a++ is Post increment operator and ++a is a preincrement operator.
Question:
int a=10;
for(int i=0,i<4,i++)<br /> System.out.println(a++ + ++a);<br /> 1.In the first iteration, the following things happen.<br /> The original value of "a i.e 10" + "a" gets incremented to "11" , then "++a increments the value of "11" by 1 to 12 and hence you get "22".<br /> So the net result is,<br /> "a" "a++" --> "++a"
A. 10 + [11]--> [11 + 1] = 22
B. 12 + [13]--> [13 + 1] = 26
C. 14 + [15]--> [15 + 1] = 30
D. 16 + [17]--> [17 + 1] = 34
- Suresh Selvaraj
 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
There was a posting by Maha.. which clearly explained ++ and -- operations..
I don't remember the link exactly. However I am just presenting u the gist of it.
In your case the calculations are carried on as follows..
10(11)+(12)12 = 22
12(13)+(14)14 = 26..
and so on.. The point here is just replace the ++ operators with incremented values in brackets and add the ones outside the brackets for result..
The rule applies for both a++ or ++a.
With
Nijeesh.
 
The two armies met. But instead of battle, they decided to eat some pie and contemplate this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic