wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Preincrement used in an expression Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Preincrement used in an expression" Watch "Preincrement used in an expression" New topic
Author

Preincrement used in an expression

Matt Kidd
Ranch Hand

Joined: Jul 17, 2002
Posts: 261
The expression is as follows:
quotient /= ++x;
All variables have an integer value of 5.

The answer given is x = 6, quotient = 0.
I get lost at some point. The way I understand preincrement (variable is incremented then reassigned before used in an expression) and postincrement (variable is used then incremented) the answer I thought it would be would be an 1 because the value is incremented then it divides.
Where am I messing up?
Chad McGowan
Ranch Hand

Joined: May 10, 2001
Posts: 265
Since quotient and x both = 5, you end up with quotient = 5/6, which as an int is 0. If you change the variables to floats, you will get quotient = .8333 and x = 6.
Chad
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9053
    
  12
quotient /= ++x;

All variables have an integer value of 5.


int quotient = 5;
int x = 5;

quotient /= ++x ;
quotient = quotient / ++x ;
x = x + 1 ;
x = 6 ;
quotient = 5 / 6 ;
quotient = 5 / 6 ;

so --
quotient = 0 ;
x = 6 ;


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Preincrement used in an expression