• 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

Postfix Increment/Decrement Operators

 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would be very grateful if someone could confirm something for me:

When using an postfix increment operator that the value to the variable increments immediatly after using the original value of the variable in the expression, and does not "wait" until the entire expression has been evaluated or statement completed.

For example:



leaves x=1 and y=1 (which it does - I've tried it).

Is this the case in ALL circumstances?

Thanks

Michal
 
Michal Charemza
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, for those with Sierra & Bates, on the bottom of p166 / top of p167 it has



And then it says


The first expression compares x and y, and the result is false, because the increment on x doesn't happen until after the == test is made



So is it true that the increment does not happen immediatly after the value is returned, but after the == test is made? (although admittedly I don't think it affects the result in this case).
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The postfix/prefix operators will always alter the value of the operand, whether they form part of an assignment (=) expression or a boolean expression (==)
 
Fletcher Estes
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

So is it true that the increment does not happen immediatly after the value is returned, but after the == test is made?



Yes, the increment will only happen after the expression is evaluated
 
Michal Charemza
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Fletcher Estes:
The postfix/prefix operators will always alter the value of the operand, whether they form part of an assignment (=) expression or a boolean expression (==)



Yes, but when does the increment occur? In the boolean express (==) is it after the boolean expression is evaluated or before?

Michal
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try reading this and see if that helps clear up your questions. If not, let me know and I'll do my best.
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Effectively does this:

in that order.
This translates into:

Therefore both end up as 1.

Had you used ++x instead of x++ the outcome would have been quite different.

evaluates to

thus
 
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone solve this..my questions are in BOLD in the code commented beside the doubted lines..


public class plusTest {
public static void main(String[] args) {
int[] a = new int[]{1, 2, 3, 4};
int b = 1;
System.out.println(b+++b); //HERE THE O/P IS 3
System.out.println("b="+b);
a[b++] = b += 0; // WHAT IS A[B++] HERE?? A[4] OR A[3]??
for(int i = 0; i < a.length; i++) {
System.out.print(a[i]);
}
}
} // WHY IS THE RESULT COMIG AS 1234



public class pp {

public static void main(String[] args) {
int x=1;
int y=2;
System.out.println(x+++y); // IS IT X++ + Y OR X + ++Y ??
System.out.println("x="+x);
System.out.println("y="+y);

}
}

HELP ME
thanx,
Deepa
 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!!

System.out.println(x+++y); // IS IT X++ + Y OR X + ++Y ??



I think, the compiler will parse (I am not sure if thats the right word) the expression and once it finds two +'s following the variable x, it accounts it for a valid postfix operator and performs an addition to y. Hence, it evaluates the expression as 'x++ + y'.

I am not entirely sure why it works like that.

I'd actually love a more comprehensive answer than this.


Cheers
 
Anand Jayaraman
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another thing!!!

 
Deepa Korecherla
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually i know how the pp class works , and the expression is evaluated as x++ + y ...And the reason for that is the ++ operator has high priority than the + operator. But the plustest class ( code given) problem is not yet solved for me...the a[b++]...will it take as a[3] or a[4]...anyone pls reply.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic