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

iOS Swift Collections and Control Flow Control Flow With Conditional Statements Logical Operators

Miguel Garcia
Miguel Garcia
1,002 Points

Question of the AND Operator

What result gives us the AND Operator if the statement is "False && False"? The whole expression is still False?

1 Answer

AND operators are short circuits for checking if the first value is false. If the first value is false, there is no reason for the language to continue parsing the rest of the statement as the rest of it no longer matters.

OR operators are short circuits for checking if the first value is true. If the first value is true, the rest of the statements can be ignored as the criteria has already been satisfied.

so:

let everythingStopsHere = false
let noneOfThisMatters = false

if everythingStopsHere && noneOfThisMatters {
   // This is not entered, no code in here will be executed
}

// everythingStopsHere is the only part that was processed

let thisIsProcessed = true
let notProcessed = false

if thisIsProcessed || notProcessed {
  // thisIsProcessed is the only part that was processed because it was true, so notProcessed was ignored
}