Chapter 19: Swift Booleans

1. What is a Boolean?

A Boolean (or Bool in Swift) is the simplest data type: It can only have two possible values:

  • true
  • false

Think of it as a light switch:

  • true = light is ON
  • false = light is OFF

Booleans are used everywhere to answer yes/no questions:

  • Is the user logged in?
  • Is it raining?
  • Does the password meet the requirements?
  • Is the form valid?
  • Should we show the premium banner?

2. How to write Booleans – basic syntax

Swift

Important facts right away:

  • The values are exactlytrue and false (lowercase, no quotes)
  • You cannot write True, TRUE, yes, 1, 0, etc.
  • Swift is very strict about this
Swift

3. Where Booleans come from in real code

From comparisons

Swift

From functions & methods

Swift

From user interface state

Swift

4. Combining Booleans – logical operators (very important!)

Swift has three main operators for working with multiple booleans:

Operator Meaning Symbol Example Result
AND Both true && isAdult && hasLicense true only if both are true
OR At least one true
NOT Reverse ! !isLoggedIn true becomes false, false becomes true

Real examples:

Swift

5. Real-life examples – very common patterns

Example 1 – Login / access control

Swift

Example 2 – Showing / hiding UI elements

Swift

Example 3 – Form validation (very common)

Swift

Example 4 – Game logic

Swift

6. Common Beginner Mistakes & How to Avoid Them

Mistake Wrong / Dangerous code Correct way Why?
Using 1 / 0 instead of true/false let active = 1 let active = true Swift is strict — 1 is Int, not Bool
Writing True / TRUE let isOK = True let isOK = true Case-sensitive — only lowercase true
Using == true everywhere if isLoggedIn == true { … } if isLoggedIn { … } Cleaner & idiomatic
Forgetting to use ! for negation if isGuest == false if !isGuest or if !isGuest { … } Much more readable
Overcomplicating conditions if (age >= 18 && hasID == true)

7. Nice-to-know shortcuts & patterns

Swift

8. Quick summary – when & where you use Bool

Situation Typical variable name Typical value source
User state isLoggedIn, isPremium, isVerified From auth / subscription
UI visibility shouldShowBanner, isLoading, hasError From view model / network state
Form validation isValid, canSubmit, termsAccepted From text fields + checkboxes
Permissions / capabilities hasCameraAccess, canDrive, isAdult From age check, permissions
Game / app logic isGameOver, levelCompleted, hasKey From game state

Which part of Booleans would you like to explore more deeply next?

  • More complex logical conditions (&&, ||, combining many conditions)
  • Booleans in SwiftUI (how they drive the UI)
  • Optional Booleans (Bool?) – when and why they appear
  • Using booleans with enums (very powerful pattern)
  • Or move to another topic (strings, arrays, optionals…)

Just tell me — we’ll continue in the same clear, patient, teacher-like style 😊

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *