Swift Logical Operators

In Swift, logical operators are used to evaluate conditions and combine or manipulate boolean values (true or false). They are essential for controlling program flow, making decisions, and evaluating complex conditions. Below, I’ll explain the primary logical operators in Swift, their functionality, and provide clear examples for each.

Logical Operators in Swift

Swift supports three main logical operators:

  1. Logical AND (&&): Returns true if both operands are true.
  2. Logical OR (||): Returns true if at least one operand is true.
  3. Logical NOT (!): Inverts the boolean value (true becomes false, and vice versa).

Additionally, Swift allows combining these operators to form complex conditions, often used in if statements, while loops, or other control flow structures.

1. Logical AND (&&)

The && operator evaluates to true only if both operands are true. If either operand is false, the result is false.

Truth Table for AND

ABA && B
truetruetrue
truefalsefalse
falsetruefalse
falsefalsefalse

Example

Swift
let isSunny = true
let isWarm = false

if isSunny && isWarm {
    print("It's a great day for a picnic!")
} else {
    print("Not ideal for a picnic.")
}
// Output: Not ideal for a picnic.
Output✓ Tested · Swift 6.4
Not ideal for a picnic.

In this example, the condition isSunny && isWarm is false because isWarm is false, so the else block executes.

2. Logical OR (||)

The || operator evaluates to true if at least one operand is true. It returns false only if both operands are false.

Truth Table for OR

ABA || B
truetruetrue
truefalsetrue
falsetruetrue
falsefalsefalse

Example

Swift
let hasCoffee = false
let hasTea = true

if hasCoffee || hasTea {
    print("You have a beverage!")
} else {
    print("No beverages available.")
}
// Output: You have a beverage!
Output✓ Tested · Swift 6.4
You have a beverage!

Here, hasCoffee || hasTea is true because hasTea is true, so the if block executes.

3. Logical NOT (!)

The ! operator inverts the boolean value of its operand. If the operand is true, it becomes false, and if it’s false, it becomes true.

Truth Table for NOT

A!A
truefalse
falsetrue

Example

Swift
let isRaining = false

if !isRaining {
    print("You can go outside without an umbrella.")
} else {
    print("Bring an umbrella!")
}
// Output: You can go outside without an umbrella.
Output✓ Tested · Swift 6.4
You can go outside without an umbrella.

In this example, !isRaining evaluates to true because isRaining is false, so the if block executes.

Combining Logical Operators

You can combine logical operators to create more complex conditions. When doing so, use parentheses to ensure clarity and control the order of evaluation. Swift evaluates ! first, followed by &&, and then ||, but parentheses make the logic explicit.

Example

Swift
let hasTicket = true
let isAdult = false
let isVIP = true

if hasTicket && (isAdult || isVIP) {
    print("You can enter the event.")
} else {
    print("Entry denied.")
}
// Output: You can enter the event.
Output✓ Tested · Swift 6.4
You can enter the event.

Explanation:

  • isAdult || isVIP evaluates to true because isVIP is true.
  • hasTicket && (isAdult || isVIP) is true because both hasTicket and the result of (isAdult || isVIP) are true.
  • The if block executes, printing "You can enter the event."

Practical Example with Multiple Operators

Here’s a more realistic example that combines all three logical operators to simulate a decision-making process:

Swift
let isWeekend = true
let hasHomework = false
let isSunny = true

if isWeekend && !hasHomework && isSunny {
    print("Go play outside!")
} else if isWeekend && !isSunny {
    print("Stay indoors and relax.")
} else {
    print("You might have other responsibilities.")
}
// Output: Go play outside!
Output✓ Tested · Swift 6.4
Go play outside!

Explanation:

  • The condition isWeekend && !hasHomework && isSunny checks if it’s the weekend, there’s no homework, and it’s sunny.
  • Since isWeekend is true, !hasHomework is true (because hasHomework is false), and isSunny is true, the entire condition is true.
  • The first if block executes.

Short-Circuit Evaluation

Swift uses short-circuit evaluation for && and ||:

  • For &&, if the first operand is false, Swift doesn’t evaluate the second operand because the result will be false regardless.
  • For ||, if the first operand is true, Swift doesn’t evaluate the second operand because the result will be true regardless.

Example

Swift
let number = 5

if number > 10 && number < 20 {
    print("Number is between 10 and 20.")
} else {
    print("Number is not in range.")
}
// Output: Number is not in range.
Output✓ Tested · Swift 6.4
Number is not in range.

Here, since number > 10 is false, Swift doesn’t check number < 20 because the && condition cannot be true.

Best Practices for Logical Operators in Swift

1. Use Parentheses for Clarity

When combining multiple logical operators, always use parentheses to explicitly define the order of evaluation. This improves readability and prevents errors due to operator precedence (! has higher precedence than &&, which has higher precedence than ||).

Swift
let isSunny = true
let isWarm = false
let isWeekend = true

// Clear with parentheses
if (isSunny && isWarm) || isWeekend {
    print("Good day to go out!")
}

// Ambiguous without parentheses (avoid)
if isSunny && isWarm || isWeekend {
    print("This might not behave as expected.")
}
Output✓ Tested · Swift 6.4
Good day to go out!
This might not behave as expected.

2. Leverage Short-Circuit Evaluation

Swift
struct User {
    let name: String
    let isActive: Bool
}

let user = User(name: "Alice", isActive: false)
if user.isActive && user.name.count > 3 {
    print("Active user with long name")
}

3. Break Down Complex Conditions

Swift
let isAdult = age >= 18
let hasTicket = ticket != nil
let isVIP = membershipLevel == .vip

if isAdult && (hasTicket || isVIP) {
    print("Access granted")
}

4. Avoid Double Negation

Swift
let isBlocked = false
if !isBlocked {
    print("User can access content")
}
Output✓ Tested · Swift 6.4
User can access content

Limitations of Logical Operators in Swift

1. Boolean-Only Operands

Swift
let number = 0
// Invalid: Cannot convert Int to Bool
// if number && true { ... }

// Solution
if number != 0 && true {
    print("Now it works")
}

2. No Bitwise Operations with Logical Operators

Swift
let a = 5 // 0101
let b = 3 // 0011
// Invalid: if a && b { ... }
if (a & b) != 0 {
    print("At least one common bit")
}
Output✓ Tested · Swift 6.4
At least one common bit

3. Short-Circuit Dependency

Swift
var counter = 0
func incrementCounter(_ x: inout Int) -> Bool {
    x += 1
    return true
}
let condition = false && incrementCounter(&counter)
print(counter) // 0 (not incremented)
Output✓ Tested · Swift 6.4
0

Example: Putting It All Together

Swift
struct User {
    let age: Int
    let hasTicket: Bool
    let membershipLevel: String?
}

func canEnterEvent(user: User) -> Bool {
    // Break down conditions for clarity
    let isAdult = user.age >= 18
    let hasValidTicket = user.hasTicket
    let isVIP = user.membershipLevel == "VIP"

    // Use parentheses for explicit logic
    let canEnter = isAdult && (hasValidTicket || isVIP)

    // Debug logging for complex conditions
    print("isAdult: \(isAdult), hasValidTicket: \(hasValidTicket), isVIP: \(isVIP)")

    return canEnter
}

let user = User(age: 20, hasTicket: false, membershipLevel: "VIP")
if canEnterEvent(user: user) {
    print("User can enter the event")
} else {
    print("Entry denied")
}
// Output: isAdult: true, hasValidTicket: false, isVIP: true
// User can enter the event
Output✓ Tested · Swift 6.4
isAdult: true, hasValidTicket: false, isVIP: true
User can enter the event

Practice Exercise: Logical Operators with Arithmetic in Swift

Question

You are creating a simple program to check if a student can join a school trip. The rules are:

  • The student must be at least 12 years old.
  • The student must have saved at least 50 points to participate.
  • The trip is only allowed if the total points (saved points plus a bonus of 10 points) are greater than or equal to 60.

Write a Swift code snippet that uses logical operators and arithmetic operators to determine if a student can join the trip. Use the following variables:

  • age (an integer representing the student's age)
  • savedPoints (an integer representing the student's saved points)

Test the condition with the following values:

  • age = 13
  • savedPoints = 45

Print a message saying "Student can join the trip!" if the conditions are met, or "Student cannot join the trip." otherwise.

Hint: Use the && operator to combine conditions and the + operator to add the bonus points.

Show solution
Swift
// Define the variables
let age = 13
let savedPoints = 45

// Calculate total points with a bonus of 10
let totalPoints = savedPoints + 10

// Check the conditions using logical operators and arithmetic
if age >= 12 && totalPoints >= 60 {
    print("Student can join the trip!")
} else {
    print("Student cannot join the trip.")
}

// Output: Student cannot join the trip.
Output✓ Tested · Swift 6.4
Student cannot join the trip.

Explanation

  1. Arithmetic Operator (+): The + operator is used to add savedPoints (45) and the bonus (10), resulting in totalPoints = 55.
  2. Logical Operators (&& and >=): The condition age >= 12 is true; totalPoints >= 60 is false; combined with && gives false.
  3. Learning Challenge: Tests understanding of combining arithmetic with logical operators in a practical scenario.
  4. Why It Works: Uses basic integers and operators introduced in earlier sections.

Tip: Try adjusting savedPoints to 50 and re-run to see how the output changes!