0Pricing
Swift Academy · Lesson

Logical Operators and Short-Circuiting

Combine conditions with &&, ||, ! and understand short-circuit evaluation.

Logical Operators and Short-Circuiting is a free Swift Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Logical Operators

Logical operators combine Bool values: AND &&, OR ||, and NOT !. They are essential for building conditions.

let isWeekend = true
let isSunny = false
print(isWeekend && isSunny)
print(isWeekend || isSunny)
print(!isSunny)

Logical NOT

The ! operator flips a boolean: true becomes false and vice versa.

let loggedIn = false
if !loggedIn {
    print("Please log in")
}

Logical AND

&& is true only when both operands are true. Use it when every condition must hold.

let hasTicket = true
let hasID = true
let canEnter = hasTicket && hasID
print("Can enter:", canEnter)

Logical OR

|| is true when at least one operand is true. Use it when any condition is enough.

let isAdmin = false
let isOwner = true
let canEdit = isAdmin || isOwner
print("Can edit:", canEdit)

Combining AND and OR

You can mix operators. && has higher precedence than ||, but parentheses make complex conditions readable.

let age = 20
let hasGuardian = false
let allowed = (age >= 18) || hasGuardian
print("Allowed:", allowed)

Short-Circuit Evaluation of AND

Swift uses short-circuiting: in a && b, if a is false, Swift never evaluates b because the result is already false.

func sideEffect() -> Bool {
    print("sideEffect ran")
    return true
}
let result = false && sideEffect()
print("Result:", result)

Short-Circuit Evaluation of OR

Similarly in a || b, if a is true, Swift skips b. The right side is only checked when needed.

func sideEffect() -> Bool {
    print("sideEffect ran")
    return false
}
let result = true || sideEffect()
print("Result:", result)

Why Short-Circuiting Matters

Short-circuiting lets you place a cheap or safe guard first, protecting an expensive or risky check that follows. Order of operands is meaningful.

let numbers = [10, 20, 30]
let index = 5
if index < numbers.count && numbers[index] > 0 {
    print("Safe access")
} else {
    print("Index out of bounds guarded")
}

Evaluation Order Left to Right

Operands are evaluated left to right. Putting the determining condition first can avoid running unnecessary or unsafe code.

func check(_ label: String, _ value: Bool) -> Bool {
    print("checking", label)
    return value
}
let r = check("A", false) && check("B", true)
print("Final:", r)

Building Complex Conditions

Real programs combine many checks. Group with parentheses to express the exact logic and keep it readable.

let temp = 25
let humidity = 40
let comfortable = (temp >= 18 && temp <= 26) && (humidity < 60)
print("Comfortable:", comfortable)

Negating Compound Conditions

Wrapping a condition in !(...) inverts the whole thing. By De Morgan rules, !(a && b) equals !a || !b.

let a = true
let b = false
print(!(a && b))
print(!a || !b)

Quick Check

Reason about short-circuit evaluation.

Recap: Logical Operators and Short-Circuiting

You learned &&, ||, and !, that && stops at the first false and || stops at the first true, and how this left-to-right short-circuiting lets a leading guard protect a following check.

let list = [1, 2, 3]
let i = 10
let safe = i < list.count && list[i] == 2
print("Safe and matched:", safe)

Frequently asked questions

Is the “Logical Operators and Short-Circuiting” lesson free?

Yes — the full text of “Logical Operators and Short-Circuiting” is free to read here on the web, and the Swift Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Swift Academy course, upgrade to CoddyKit PRO.

What will I learn in “Logical Operators and Short-Circuiting”?

Combine conditions with &&, ||, ! and understand short-circuit evaluation. You practise Swift Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Swift Academy?

No prior experience is required. Swift Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Logical Operators and Short-Circuiting” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Swift Academy lesson?

Yes. Every Swift Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Arithmetic and Compound Assignment Operators
  2. Comparison and Identity Operators
  3. Logical Operators and Short-Circuiting
  4. Bitwise and Overflow Operators
← Back to Swift Academy