• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

post increment  RSS feed

 
Ranch Hand
Posts: 689
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


hi why it is displaying 10 in the 2nd,3rd output lines
 
Ranch Hand
Posts: 210
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As long as you place the variable which is being post incremented in the lhs also ( i = i++), the value will not change since the incremented value is discarded.
 
Aum Tao
Ranch Hand
Posts: 210
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, check out this link

http://www.coderanch.com/t/193789/java-programmer-SCJP/certification/postfix-increment
 
saikrishna cinux
Ranch Hand
Posts: 689
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but why it is displaying 10 for 3 times for the second time it must display 11 but it is not .
i think there some problem with java compiler
 
Ranch Hand
Posts: 2023
 
Ranch Hand
Posts: 1249
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by saikrishna cinux:
but why it is displaying 10 for 3 times for the second time it must display 11 but it is not .
i think there some problem with java compiler



Saikrishna,

check this link

Postincrement and Preincrement Operators.

Check this discussion it will help you a lot.
 
Rancher
Posts: 42973
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

i think there some problem with java compiler


Compiler bugs are quite rare. That's not to say they can't happen, just that they are highly unlikely, especially if the compiler is as widely used as javac. In 20 years I've had a single language for which the compilers were faulty due to their immaturity (HPF a.k.a High Performance Fortran, if you're curious).
I've learned to double- and triple-check what I'm doing whenever I suspect a compiler bug, and -with the exception mentioned above- it always turned out to be me
 
Ranch Hand
Posts: 387
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Value of postincement in not available to current "statement" (ending with ; sign is a statement).
So what ever is inceremnted will be effective in next statement.
so if we have



in line 1 i is incremented. No probs here...BUT remember that incremented value is available from next statement onwards (thats the catch).

this is wht complier must be thinking...
Compiler: Ok [line 1] I will increment i++ so my i is 1 now but this updated value will be available only to next statement NOT current statement.
Compiler: Ok [line 1] I will assign the RHS to the LHS.
RHS = i++ result=> value in current statement =>0
:. LHS = 0

Please correct me if I am wrong!
Thanks!
Rohit
[ July 17, 2006: Message edited by: Rohit Kharat ]
 
saikrishna cinux
Ranch Hand
Posts: 689
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Shaan Shar
Ranch Hand
Posts: 1249
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by saikrishna cinux:




Are you still not clear..

:roll: :roll:
 
Rohit Nath
Ranch Hand
Posts: 387
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
consider statement i=i++;
Statement 1 can be divided into two parts.

Statement1
---------------------------
| i ++ | Assign LHS <= RHS | => o/p ?
---------------------------

Statement2
----------------------------
| System.out.println(i) | => o/p?
----------------------------

Now you solve this statement with i=0.
****Remember
1) the box indicates a single statement.
2) posstincrement result is available to next statement not current one
means in box 1 even if i is incremented it value will be 0 only withing the "box" statement.


I think once you go through this it should be clear I hope.
Thx!

[ July 17, 2006: Message edited by: Rohit Kharat ]
[ July 17, 2006: Message edited by: Rohit Kharat ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!