• 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

Post increment operator

 
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

When I do workout on the below program




It gave me result 121.My way of working logic is
i=0
a=0

i=i++ i take this like
i=i=i+1
i=0(i is incremented 1 here)

then,when we come to print i,it will print i=0

a=i++; I take this like
a=i=i+1
a=1 ,i=1+1(why am I taking like this means that I value is assigned to new variable a.so I took this statement as two(a=1,i=2).
When we come to print i,a=it prints 2,1


So my calculated output is 021.But when I run this program it gives the result 010.Can someone please explain that in my way of understanding.


Thanks in advance.
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


In the first increment statement, i = i++, the value of i++ is the value before the increment takes place. Since the left-hand side is the same variable, it's like the increment didn't happen. So i is 0.

In the line a = i++, a is the value of i before the increment takes place, so a is 0. Since we don't modify the value of i after the increment, i is 1.

Now a is 0.

So the output is

010
[ February 27, 2006: Message edited by: Keith Lynn ]
 
Shiva Mohan
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
but i thought a=1 .because i's value is incremented in the below statement right.Then how come we take a=0.I am stillin cofusion.Please help me.
 
Keith Lynn
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The right-hand side of the statement is i++, which would normally increment i's value by 1. But the left-hand side of the statement is the variable i. The value of i++ as an expression is the value of i before the increment. So essentially you are setting i to the value it has before the increment.
 
Shiva Mohan
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oh......Now i got it.Thanks.
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the code if we replace i=i++ with i=++i we will get 1 2 1 as the output if you understand the previous explanation then give me explanation for this.
thanks a lot
 
Put a gun against his head, pulled my trigger, now he's dead, that tiny ad sure bled
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic