Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Jorge Alvarez
9,330 Pointsunderstanding java syntax
assume that x and y have been defined and initialized as int values. The expression
!(!(x != y ) && (y > 7))
Im not sure what the code above is equivalent too? maybe..
(x == y) && (y > 7)
any ideas?
2 Answers
Ken Alger
Treehouse TeacherJorge;
I am a bit rusty on my boolean algebra and De Morgan's laws, but a quick Wikipedia search provides these rules in English
The negation of a conjunction is the disjunction of the negations.
The negation of a disjunction is the conjunction of the negations.
Or not(A and B) is the same as (not A) or (not B)... and not(A or B) is the same as (not A) and (not B)
Here's your prompt
!(!(x != y ) && (y > 7))
Following De Morgan's laws then and breaking this apart piece by piece, we should be able to sort through all of those exclamation marks.
Your prompt would fall into the first example above of not(A and B), so we should be able to rewrite it as
(x !=y) || !(y > 7)
Correct? The A portion would literally be not(not A), so I would think that it would just be A, or in this example (x != y). The B portion seems rather straight forward.
Post back with your thoughts as I could certainly be off on my interpretation and extrapolation of all those negations.
Ken
Axel McCode
13,869 Pointsint x = 1;
int y = 8;
!(!(x != y ) && (y > 7))
// In this example y is greater than 7, so the right side of the conditional operator (&&) will be true.
// On the left side x != y would be true however it is false because there is a "not" operator (!) before that conditional.
// If both sides of the && operator are true, (x and y would have to be equal and y greater than 7)
// that whole conditional statement would be false because there is another ! operator before the whole conditional statement
Hope this helps :)
Jorge Alvarez
9,330 Pointsso (x != y) || (y < 7)
Axel McCode
13,869 PointsSimplified, it would look like this ---> !( (x == y) && (y > 7) ) And this would return true if both sides of the && operator return false. Remember there is a "not" operator right before the opening parenthesis.
( (x == y) && (y > 7) ) This will always return false if it is true and vice versa, again because of the "not" operator before it. ---> ! ( (x == y) && (y > 7) )
Jorge Alvarez
9,330 PointsThanks for the input man! happy first day of 2015!!
Jorge Alvarez
9,330 PointsJorge Alvarez
9,330 Pointslets just see if I got this down so would (x !=y) || !(y > 7) become (x != y) || (y <= 7) I was trying to get rid of the exclamation mark in !(y > 7)
Ken Alger
Treehouse TeacherKen Alger
Treehouse TeacherJorge,
Looks like you got some of your statement cut off.
!(y > 7) reads y is **not* greater than 7*, you could express that with ( y <= 7 ) as well.
So if your example was in an if statement:
That would run your magical code as long as
xwas not equal toy, orywas not greater than 7.Make any sense?
Ken
Jorge Alvarez
9,330 PointsJorge Alvarez
9,330 Pointsahh yeah thats what I was trying to figure out!!! your a life saver thanks man this whole thing is sort of a brain twister to a beginner like me but yes Thank you! happy first day of 2015!
Ken Alger
Treehouse TeacherKen Alger
Treehouse TeacherThey still make my head hurt as well. I have always hated coming across (or having to write) code that has multiple negatives in the same line. I find it highly unreadable in general and find myself either rewriting it, i.e. !( y>7) as (y <= 7) and/or making multiple if... else clauses. Perhaps not the most efficient, but if I have to write comments like:
I figure it is probably not the best code to use.
Ken
Jorge Alvarez
9,330 PointsJorge Alvarez
9,330 Pointsahaha "In the future only God will know" perhaps coding can drive people insane