• 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

XOR Question

 
Ranch Hand
Posts: 32
Eclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Why the value of x is 4 and not 7 ?
The way i see it, in the first if 4 > x is true

then ((++x + 2) > 3 )also returns true and x is now 4
the second if 4 > ++x is false and x is now 5, then (++x == 5) is false but because of the ! is true so the if passes and x is now 6
then x gets incremented again and its now 7

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Both expressions in both of your exclusive-or statements evaluate to true, so the XOR is false. That means x is incremented only twice.

(4 > x) is true... ((++x + 2) > 3) increments x to 2, and 2 + 2 > 3. Both sides are true, the XOR is not satisfied, so x is not incremented again.

Now we have (4 > ++x), which increments x to 3, and the expression is true...
And the last expression is !(++x == 5). We increment x to 4, and !(4 == 5) is true. So x is incremented no more.

 
Mauro Mazzucco
Ranch Hand
Posts: 32
Eclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[quote=Howard Troxler ((++x + 2) > 3) increments x to 2, and 2 + 2 > 3.

why inst the +2 being add to x?
 
Howard Troxler
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's just an expression, not an assignment, isn't it?

In the expression (++x + 2), the only thing happening to x is that it is being incremented by 1. The expression itself evaluates to whatever the resulting value of x is, plus 2, but the 2 is not being added to x....

 
reply
    Bookmark Topic Watch Topic
  • New Topic