Bird
Raised Fist0
Swiftprogramming~5 mins

Logical operators in Swift - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
What does the logical AND operator (&&) do in Swift?
The logical AND operator (&&) returns true only if both conditions on its sides are true. If either side is false, the whole expression is false.
Click to reveal answer
beginner
Explain the logical OR operator (||) in Swift.
The logical OR operator (||) returns true if at least one of the conditions is true. It returns false only if both conditions are false.
Click to reveal answer
beginner
What does the logical NOT operator (!) do in Swift?
The logical NOT operator (!) reverses the value of a Boolean. If the value is true, it becomes false; if false, it becomes true.
Click to reveal answer
intermediate
How would you combine multiple logical operators in Swift?
You can combine multiple logical operators by using parentheses to group conditions and control the order of evaluation, just like in math.
Click to reveal answer
intermediate
What is short-circuit evaluation in logical operators?
Short-circuit evaluation means Swift stops checking conditions as soon as the result is known. For example, in AND (&&), if the first condition is false, it won’t check the second.
Click to reveal answer
What is the result of true && false in Swift?
Aerror
Btrue
Cnil
Dfalse
Which operator returns true if at least one condition is true?
A||
B&&
C!
D==
What does !true evaluate to in Swift?
Afalse
Btrue
Cnil
Derror
In the expression (false || true) && false, what is the result?
Atrue
Bfalse
Cnil
Derror
What happens in short-circuit evaluation with the expression false && someFunction()?
AError occurs
BsomeFunction() is called
CsomeFunction() is not called
DResult is always true
Describe how the logical AND (&&), OR (||), and NOT (!) operators work in Swift.
Think about true and false combinations.
You got /3 concepts.
    Explain what short-circuit evaluation means and why it is useful in Swift logical operations.
    Imagine checking conditions like a checklist that stops early.
    You got /3 concepts.

      Practice

      (1/5)
      1. Which logical operator in Swift returns true only if both conditions are true?
      easy
      A. == (Equality)
      B. || (OR)
      C. && (AND)
      D. ! (NOT)

      Solution

      1. Step 1: Understand the AND operator

        The AND operator (&&) returns true only when both conditions it connects are true.
      2. Step 2: Compare with other operators

        The OR operator (||) returns true if at least one condition is true, NOT (!) flips true to false and vice versa, and == checks equality, not logical combination.
      3. Final Answer:

        && (AND) -> Option C
      4. Quick Check:

        AND = && [OK]
      Hint: AND needs both true; look for && operator [OK]
      Common Mistakes:
      • Confusing AND (&&) with OR (||)
      • Thinking NOT (!) combines conditions
      • Mixing equality (==) with logical operators
      2. Which of the following is the correct syntax to negate a Boolean value isActive in Swift?
      easy
      A. !isActive
      B. not isActive
      C. isActive!
      D. isActive.not()

      Solution

      1. Step 1: Recall the NOT operator syntax

        In Swift, the NOT operator is written as an exclamation mark (!) placed before the Boolean variable to flip its value.
      2. Step 2: Check each option

        !isActive is the correct syntax. The other options are not valid Swift syntax for negation.
      3. Final Answer:

        !isActive -> Option A
      4. Quick Check:

        NOT = ! before variable [OK]
      Hint: Use ! before variable to negate Boolean [OK]
      Common Mistakes:
      • Writing 'not' instead of '!'
      • Putting ! after variable name
      • Using method calls like .not()
      3. What will be the output of this Swift code?
      let a = true
      let b = false
      print(a || b && !b)
      medium
      A. Syntax Error
      B. true
      C. false
      D. nil

      Solution

      1. Step 1: Evaluate the NOT operator

        !b means !false which is true.
      2. Step 2: Evaluate AND operator

        b && !b is false && true which is false.
      3. Step 3: Evaluate OR operator

        a || (result of AND) is true || false which is true.
      4. Final Answer:

        true -> Option B
      5. Quick Check:

        OR with true = true [OK]
      Hint: Remember NOT first, then AND, then OR [OK]
      Common Mistakes:
      • Ignoring operator precedence
      • Evaluating OR before AND
      • Misunderstanding ! operator
      4. Identify the error in this Swift code snippet:
      let x = true
      let y = false
      if x || && y {
          print("Yes")
      }
      medium
      A. Missing parentheses around conditions
      B. print statement syntax error
      C. Variables x and y are not Boolean
      D. Incorrect use of logical operators together

      Solution

      1. Step 1: Analyze the if condition syntax

        The condition uses 'x || && y' which combines OR (||) and AND (&&) operators without a proper operand between them.
      2. Step 2: Identify the logical operator misuse

        Logical operators must connect two Boolean expressions. Here, '|| &&' is invalid syntax.
      3. Final Answer:

        Incorrect use of logical operators together -> Option D
      4. Quick Check:

        Operators must connect expressions properly [OK]
      Hint: Operators need operands on both sides [OK]
      Common Mistakes:
      • Writing two operators without a condition between
      • Assuming parentheses fix operator misuse
      • Confusing variable types with operator errors
      5. You want to check if a number n is between 10 and 20 inclusive in Swift. Which condition correctly uses logical operators?
      hard
      A. n >= 10 && n <= 20
      B. n > 10 && n < 20
      C. n >= 10 || n <= 20
      D. !(n < 10 && n > 20)

      Solution

      1. Step 1: Understand the range condition

        To check if n is between 10 and 20 inclusive, n must be greater than or equal to 10 AND less than or equal to 20.
      2. Step 2: Evaluate each option

        n >= 10 || n <= 20 uses OR which is incorrect because it allows values outside the range. n > 10 && n < 20 excludes 10 and 20 by using > and <. !(n < 10 && n > 20) negates an impossible condition and is incorrect.
      3. Final Answer:

        n >= 10 && n <= 20 -> Option A
      4. Quick Check:

        Use AND for range checks [OK]
      Hint: Use && to combine lower and upper bounds [OK]
      Common Mistakes:
      • Using OR instead of AND for range
      • Excluding boundary values by using > or <
      • Using negation incorrectly for range
      Recommended

      Ready to prove it?

      Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

      10 questions~5 mineasy hard