This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes pre/post incrementing in an if statement Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "pre/post incrementing in an if statement" Watch "pre/post incrementing in an if statement" New topic
Author

pre/post incrementing in an if statement

Leroy J Brown
Ranch Hand

Joined: Dec 02, 2007
Posts: 71
There was a post a while ago here that made me aware of something basic that I lack a strong grasp on and I'd like to make sure I know exactly whats going on. I've adapted a bit of the code to my purposes here.



Using increment operators inside the if statement's parenthesis (if there is a more terse or technical way to refer to the testing area of an if, while, etc I would also appreciate someone letting me know) seems very odd to me. Will the if statement actually increment the z variable as is being interpreted, and in order? Meaning since z is 43 before the statement's execution the the first statement in the parenthesis should be false but then increment z to 44. The second statement in the parenthesis should then increment z to 45 and and then return true, making the if statement true. Is that correct?

-thanks
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19672
    
  18

This is the full order of your code:

- z becomes 43
- the value of z is compared to 44, resulting in false
- z is increased (postfix) to 44
- the first part evaluates to false so the second part is evaluated
- z is increased (prefix) to 45
- the value of z is compared to 45, resulting in true
- the second part evaluates to true so the if-body is executed
- z is increased (postfix) to 46


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Katrina Owen
Sheriff

Joined: Nov 03, 2006
Posts: 1358
    
  17
Originally posted by Tristan Rouse:
Using increment operators inside the if statement's parenthesis [...] seems very odd to me.


Though it can be done, it doesn't lend itself to readability, so I personally would avoid it.

Will the if statement actually increment the z variable as is being interpreted, and in order? Meaning since z is 43 before the statement's execution the the first statement in the parenthesis should be false but then increment z to 44. The second statement in the parenthesis should then increment z to 45 and and then return true, making the if statement true. Is that correct?


You got it



(if there is a more terse or technical way to refer to the testing area of an if, while, etc I would also appreciate someone letting me know)


I guess you could refer to it as the "if condition". I'm not sure if it is standard, though.
Leroy J Brown
Ranch Hand

Joined: Dec 02, 2007
Posts: 71
Thanks a bunch for your help!
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Rob Prime:
This is the full order of your code:

- z becomes 43
- the value of z is compared to 44, resulting in false
- z is increased (postfix) to 44
- ...


That's actually not correct. What really happens is:

- z becomes 43
- z is increased (postfix) to 44
- the value of z before the increase is compared to 44, resulting in false
- ...


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
 
 
subject: pre/post incrementing in an if statement