• 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

confusion with unary operator : ++

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Following code is producing z = 0 as a result.
Can any buddy justify this ...?


[ February 26, 2007: Message edited by: Vishal K Patel ]
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
//courtesy siva koppala
try this out �����then definitely you will be knowing what is happening.

class A

{

public static void main(String[] arg)

{

int z=0;

for(int k=0;k<10;k++)

z=++z;

System.out.println("z = " + z);

}

}
 
Vishal K Patel
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well...

It is producing z = 10.
Actually I am expecting the same result from


My understaing is,
z will get incremented after the whole expression will be executed.
So in the next iteration z will have the latest value.

Can you make me clear...?
[ February 26, 2007: Message edited by: Vishal K Patel ]
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Same topic: increment++
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

z will get incremented after the whole expression will be executed.
So in the next iteration z will have the latest value.


No. Check the other thread, and the FAQ they link to in that thread.
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

In this case z is incremented but it returns the previous value of z.
i.e.

so z contains the same old value.



In this case the value returned is the incremented value of z;

i.e.

[ February 26, 2007: Message edited by: Sundaramoorthy Thuwaragan ]
reply
    Bookmark Topic Watch Topic
  • New Topic