This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds and have James Denton on-line!
See this thread for details.
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • 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:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Integer Mistry  RSS feed

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi i m Rizwan Ahmed Uppal i face a problem during coding. problem is
Execute the code
int i=0;
i=i++;
System.out.println(i);

it should print 1 but it never prints 1 it always print 0.because one time i increment but answer is still amgious.I dont know y?.Is there some one have anwser?
sorry u all foks my question is that i=i++; is a complete statement and if it executed then i must be incremented one time i m printing i's value after increament.so it should be 1.

[ December 18, 2002: Message edited by: Rizwan Ahmed Uppal ]
 
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
int i=0;
Assigns 0 to the variable i.
i=i++;
The right hand side increments the variable i by one, giving it the value 1. Since you used the increment postfix operator, the value of the right hand side is the value before the increment, i.e. 0. This value is then assigned to the variable i.
Remove "i=".
- Peter
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
That's the basic fundamental of postfix and prefix operator. The unary operator u have tried is postfix. It'll never give the value 1 for the first time.If u want the result to be 1, then use ++i instead of i++.
Regards
Nayan.
 
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

A ) i is gonna get some value, dont know wich,
B ) The value i has is 0 ( this is the value A is going to get)
C ) i is incremented by 1 thus i == 1
now that everiting is evaluated the assignations take place...
i ( A ) takes the value calculated in ( B ) which is 0
Now
printing i result on 0

Thats how I see it
=============
Zkr Ryz
[ December 19, 2002: Message edited by: Zkr Ryz ]
 
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!