• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

pre/post incrementing in an if statement

 
Leroy J Brown
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Katrina Owen
Sheriff
Posts: 1367
18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a bunch for your help!
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
- ...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic