• 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

incremetor

 
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here are two programs using the incrementor x++ or x-- in a different way:
Program1:
class incDec
{
public static void main(String args[]) {
int x;
int y;
x = 1;
y = x++;
System.out.print(y + "\n");
x = 1;
y = ++x;
System.out.print(y + "\n");
x = 1;
y = x--;
System.out.print(y + "\n");
x = 1;
y = --x;
System.out.print(y + "\n");
}
}
Program 2:
class incdem
{
public static void main(String args[]) {
int x;
int y;
x = 1;
x = x++;
System.out.print(x + "\n");
x = 1;
x = ++x;
System.out.print(x + "\n");
x = 1;
x = x--;
System.out.print(x + "\n");
x = 1;
x = --x;
System.out.print(x + "\n");
}
}
=> Both programms use the incrementor in the way: x = x ++
y = x ++
Result of both programms: 1 - 2 - 1 -
---------
program 3:
class incDe
{
public static void main(String args[]) {
int x;
int y;
x = 1;
x++;
System.out.print(x + "\n");
x = 1;
++x;
System.out.print(x + "\n");
x = 1;
x--;
System.out.print(x + "\n");
x = 1;
--x;
System.out.print(x + "\n");
}
}
Uses incrementor in the way: x++, x++ (instead of y = x++)
Result: 2 - 2 - 0 - 0
*************************************************
Why the different results between
x++
System.out.println(x)
and
y = x++
System.out.println(x)
???
******************************************+

 
Thomas Markl
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the result of program 1+2 is
1-2-1-0
 
Ranch Hand
Posts: 1012
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the increment is performed before the value is assigned if it is like this:
x = ++x;
otherwise, it performed after the value is assigned. so, when it says:
x = 1;
x = ++x;
x will be equal to 2 when it is printed. but, when it is like:
x = 1;
x = x++;
x is still equal to 1 because the increment was performed after the value was assigned.
same thing with y... and same thing with --.
 
Thomas Markl
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Thomas Markl:
the result of program 1+2 is
1-2-1-0


 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic