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
Tomas Salas
Front End Web Development Techdegree Graduate 37,934 PointsDon't get a question with logic operators in Swift
What does the following statement evaluate to? (!true || !false) && (true && !false)
I mean I somewhat know what is happening here, but how do you "evaluate" this ? the answers are truth and false. Truth being the correct one, but why is the correct one ?
can somebody explain me in simple terms ? it will be most appreciated.
3 Answers
Alex Hedley
16,381 PointsIf you take one at a time (!true || !false) && (true && !false)
(false || !false) && (true && !false)
(false || true) && (true && !false)
(true) && (true && !false)
(true) && (true && true)
(true) && (true)
true
For and (&&) both have to be the same For or (||) if one is true then true for not (!) reverse the value
Logical Operators https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators
| Operator | Usage | Description |
|---|---|---|
| Logical AND (&&) | expr1&& expr2 | Returns expr1 if it can be converted to false; otherwise, returns expr2. Thus, when used with Boolean values, && returns true if both operands can be converted to true; otherwise, returns false. |
| Logical OR ( | ) | |
| Logical NOT (!) | !expr | Returns false if its single operand can be converted to true; otherwise, returns true. |
Jaime Smith
2,097 PointsThink of the AND logic like a logging into a website, you have a username and a password. If you enter the username AND password correctly, you'll always be able to log into the site because both fields were evaluated to true.
The OR logic just stays either item can be true for the statement to be true.
! just means the opposite of whatever is written... so !true = false (AKA nottrue = false)
Tomas Salas
Front End Web Development Techdegree Graduate 37,934 PointsTHX guys, coding requires a lot knowledge, nevertheless I shall get there eventually.