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. This is lesson 3 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, and your progress syncs across the web and the CoddyKit app. The Swift Academy course includes 4 lessons in total.
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)Learn Swift with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 122
- Lessons
- 409
Frequently Asked Questions
Is the “Logical Operators and Short-Circuiting” lesson free?
Yes — any 3 lessons of the Swift Academy learning path, including “Logical Operators and Short-Circuiting”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Swift Academy course includes 4 lessons in total.
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, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 4.
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
- Arithmetic and Compound Assignment Operators
- Comparison and Identity Operators
- Logical Operators and Short-Circuiting
- Bitwise and Overflow Operators