Bool Values and Expressions
Create and combine Boolean expressions.
Bool Values and Expressions is a free Swift Academy lesson on CoddyKit — lesson 1 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.
The Bool Type
Swift has a dedicated type for truth values called Bool. A Bool can only ever hold one of two values: true or false.
You declare a Bool just like any other constant or variable.
let isLoggedIn = true
let hasError = false
print(isLoggedIn)
print(hasError)Booleans From Comparisons
Comparison operators like ==, !=, <, >, <= and >= all produce a Bool result.
The expression on the right is evaluated and stored as a true/false value.
let age = 20
let isAdult = age >= 18
let isTeen = age < 13
print(isAdult)
print(isTeen)The NOT Operator
The ! operator flips a Bool: !true becomes false and !false becomes true.
Read ! as "not".
let isOnline = false
print(!isOnline)
let isOffline = !isOnline
print(isOffline)The AND Operator
The && operator returns true only when both sides are true. If either side is false, the whole expression is false.
let hasTicket = true
let hasID = true
let canEnter = hasTicket && hasID
print(canEnter)The OR Operator
The || operator returns true when at least one side is true. It is only false when both sides are false.
let isWeekend = false
let isHoliday = true
let canRelax = isWeekend || isHoliday
print(canRelax)Combining Expressions
You can combine multiple comparisons with && and || to build richer conditions.
Each comparison produces a Bool, and the logical operators combine them.
let temp = 22
let isComfortable = temp >= 18 && temp <= 26
print(isComfortable)Using Parentheses
When mixing && and ||, use parentheses to make the grouping explicit and readable.
Swift evaluates && before ||, but being explicit avoids confusion.
let isAdmin = false
let isOwner = true
let isActive = true
let canEdit = (isAdmin || isOwner) && isActive
print(canEdit)Short-Circuit Evaluation
Swift uses short-circuit evaluation. With &&, if the left side is false the right side is never checked. With ||, if the left side is true the right side is skipped.
func check() -> Bool {
print("check ran")
return true
}
let result = false && check()
print(result)Booleans in if Statements
A Bool is exactly what an if statement needs. You can pass a Bool variable directly without comparing it to true.
let isReady = true
if isReady {
print("Go")
} else {
print("Wait")
}Storing Logic Results
Because conditions are just values, you can store a complex condition in a well-named constant. This makes the following code self-documenting.
let score = 85
let attempts = 2
let passed = score >= 60 && attempts <= 3
if passed {
print("Passed")
}Boolean Equality
You can compare two Bool values with == and != just like numbers. This is useful when matching against an expected flag.
let expected = true
let actual = (10 > 5)
print(expected == actual)
print(expected != actual)Quick Check
Test your understanding of Boolean expressions.
Recap: Boolean Expressions
You learned that Bool holds true or false, that comparisons produce Bools, and that !, && and || combine them.
Storing conditions in named constants keeps your code readable.
let inStock = true
let onSale = false
let shouldBuy = inStock && (onSale || true)
print(shouldBuy)Frequently asked questions
Is the “Bool Values and Expressions” lesson free?
Yes — the full text of “Bool Values and Expressions” 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 “Bool Values and Expressions”?
Create and combine Boolean expressions. 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 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Bool Values and Expressions” 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
- Bool Values and Expressions
- The Ternary Conditional Operator
- Avoiding Nested Ternaries
- Boolean Methods and Toggling