← Back to Swift Course | Chapter 2: Variables & Types | Lesson 6 of 8

Booleans

A boolean is a value that can only ever be true or false, like answering yes or no to a question.

Declaring Booleans

A Bool variable holds either true or false, often as the result of a comparison.

Example: Declaring Booleans

markup
let isSwiftFun = true
let isDifficult = false
print("Is Swift fun? \(isSwiftFun)")
print("Is it too difficult? \(isDifficult)")

Comparison Operators Produce Booleans

Comparing two values with ==, !=, <, >, <=, or >= always produces a Bool result.

Example: Comparison Operators Produce Booleans

markup
let age = 20
let isAdult = age >= 18
print("Is an adult: \(isAdult)")

Combining Booleans with Logical Operators

The && (logical AND), || (logical OR), and ! (logical NOT) operators combine or invert boolean values to build more complex conditions.

Example: Combining Booleans with Logical Operators

markup
let hasTicket = true
let hasID = false
let canEnter = hasTicket && hasID
print("Can enter: \(canEnter)")
print("Missing ID: \(!hasID)")
Common Mistakes
  1. Comparing a boolean to true explicitly, e.g. if isReady == true, instead of the cleaner if isReady.
  2. Assuming any non-zero number counts as true like in C; Swift's Bool is a distinct type and numbers are never implicitly converted to booleans.
  3. Forgetting that && and || short-circuit, so a later expression might not even be evaluated.
Chapter Summary
  • Bool has exactly two values: true and false.
  • Comparison operators (==, <, >, etc.) produce Bool results.
  • Logical operators && (and), || (or), and ! (not) combine boolean values.
  • Unlike C, Swift never treats numbers as implicitly true or false.
🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.