Swift: Swift Conditionals

Conditionals let your program react differently to different situations, just like a traffic light changes signals based on traffic. This lesson covers all conditional control mechanisms in Swift: if-else, switch, and more.

1. What You'll Learn


2. A True Story: A Backend Developer

(1) The Pain Point: Multi-Layer Role-Based Permissions Turned Code into Spaghetti

Bob is building a user permission system at a SaaS company. The system has 5 user roles (admin / editor / viewer / guest / banned), each with different access levels to over 20 API endpoints. Bob initially used 15 layers of nested if-else:

SWIFT
if role == "admin" {
    if action == "delete" {
        // allow
    } else if action == "edit" {
        // allow
    }
} else if role == "editor" {
    // 700 lines of repeated code...
}

The code quickly ballooned to over 700 lines. Adding a new role meant modifying 5 different places. Bob spent a full week fixing a security vulnerability caused by incorrect if-else ordering.

(2) The Solution: switch

Bob refactored the permission system using switch statements:

SWIFT
let role = "editor"
switch role {
case "admin":
    print("Full access granted")
case "editor":
    print("Read and write access")
case "viewer", "guest":
    print("Read-only access")
case "banned":
    print("Access denied")
default:
    print("Unknown role")
}

(3) The Result: Code Shrank 80%, Logic Became Crystal Clear

Dimension 15-Layer if-else After switch Refactor
Lines of code 700+ 150
Time to add a new role 30 minutes 3 minutes
Logic errors 3 per month 0 per month
Readability score 3/10 9/10

3. if/else Conditional Statements

if/else is Swift's most fundamental conditional control structure. It decides which code block to execute based on a Bool value.

100%
graph TB
    A[Condition] -->|true| B[if block]
    A -->|false| C[else if / else block]
    B --> D[Continue]
    C --> D
Syntax Description Example
if condition { } Execute when condition is true if score >= 60 { }
if ... else { } Execute else when condition is false if ... else { }
if ... else if ... else { } Check multiple conditions in order if ... else if ... else { }

(1) Basic if and else

SWIFT
let temperature = 30
if temperature > 25 {
    print("It's hot outside")
} else {
    print("It's cool outside")
}

(2) Multiple Conditions with else if

SWIFT
let score = 85
if score >= 90 {
    print("Grade: A")
} else if score >= 80 {
    print("Grade: B")
} else if score >= 70 {
    print("Grade: C")
} else if score >= 60 {
    print("Grade: D")
} else {
    print("Grade: F")
}

▶ Example: Login Status Check

SWIFT
// ============================================
// Display different messages based on login status
// ============================================
let isLoggedIn = true
let hasProfile = false
if isLoggedIn {
    print("Welcome back!")
    if hasProfile {
        print("Your profile is complete")
    } else {
        print("Please complete your profile")
    }
} else {
    print("Please log in first")
}

Output:

TEXT 📖 Display only
Welcome back!
Please complete your profile

4. switch Multi-Branch Matching

switch is a more powerful multi-branch matching tool than if-else. Swift's switch does not require break—execution automatically exits after a match.

100%
graph TB
    A[Value] --> B[case 1]
    A --> C[case 2]
    A --> D[case 3]
    A --> E[default]
    B --> F[Execute and Exit]
    C --> F
    D --> F
    E --> F
Feature Swift switch C / Java switch
Implicit break Automatic (no break needed) Must write break
Range matching Supports ... and ..< Not supported
Compound matching Comma-separated values Relies on fallthrough
Exhaustive Must cover all possibilities Not enforced
Default branch Uses default Uses default

(1) Basic switch Syntax

SWIFT
let fruit = "apple"
switch fruit {
case "apple":
    print("It's an apple")
case "banana":
    print("It's a banana")
case "orange":
    print("It's an orange")
default:
    print("Unknown fruit")
}

(2) Range Matching and Compound Matching

SWIFT
let age = 25
switch age {
case 0..<13:
    print("Child")
case 13..<20:
    print("Teenager")
case 20..<65:
    print("Adult")
case 65...:
    print("Senior")
default:
    print("Invalid age")
}

▶ Example: HTTP Status Code Handling

SWIFT
// ============================================
// Handle HTTP response status codes with switch
// ============================================
let statusCode = 404
switch statusCode {
case 100..<200:
    print("Informational")
case 200..<300:
    print("Success")
case 300..<400:
    print("Redirection")
case 400..<500:
    print("Client error")
    if statusCode == 404 {
        print("Resource not found")
    }
case 500..<600:
    print("Server error")
default:
    print("Unknown status code")
}

Output:

TEXT 📖 Display only
Client error
Resource not found

5. Advanced Control: fallthrough, where, and the Ternary Operator

Swift provides additional tools to make conditional logic more flexible.

Tool Purpose Example
fallthrough Fall through to the next case in switch case "a": fallthrough
where Add extra filtering to a condition case let x where x > 10:
Ternary ? : Concise either-or choice let max = a > b ? a : b

(1) fallthrough

SWIFT
let number = 2
switch number {
case 1:
    print("One")
case 2:
    print("Two")
    fallthrough
case 3:
    print("Three or fell through from two")
default:
    print("Other")
}

(2) where Condition Filtering

SWIFT
let point = (x: 3, y: 4)
switch point {
case let (x, y) where x == y:
    print("On the diagonal")
case let (x, y) where x > y:
    print("X is larger")
case let (x, y) where x < y:
    print("Y is larger")
default:
    print("On an axis")
}

(3) The Ternary Conditional Operator

SWIFT
let isMember = true
let discount = isMember ? 0.2 : 0.0
print("Discount: \(discount * 100)%")

▶ Example: Order Discount Calculator

SWIFT
// ============================================
// Combine if/switch/ternary to calculate order discounts
// ============================================
let orderTotal = 250.0
let customerTier = "gold"
// Ternary operator: base discount
let baseDiscount = orderTotal > 100 ? 0.05 : 0.0
// switch: membership tier discount
let tierDiscount: Double
switch customerTier {
case "platinum":
    tierDiscount = 0.20
case "gold":
    tierDiscount = 0.15
case "silver":
    tierDiscount = 0.10
default:
    tierDiscount = 0.0
}
// if: cap the combined discount
let totalDiscount = baseDiscount + tierDiscount
let finalDiscount = totalDiscount > 0.3 ? 0.3 : totalDiscount
let finalPrice = orderTotal * (1 - finalDiscount)
print("Order total: $\(orderTotal)")
print("Tier: \(customerTier)")
print("Discount: \(Int(finalDiscount * 100))%")
print("Final price: $\(finalPrice)")

Output:

TEXT 📖 Display only
Order total: $250.0
Tier: gold
Discount: 20%
Final price: $200.0

6. Full Example: User Permission Management System

SWIFT
// ============================================
// User permission management system
// Integrates if/switch/where/ternary operators
// ============================================
import Foundation
enum UserRole {
    case admin, editor, viewer, guest, banned
}
enum ActionResult {
    case granted, denied(String)
}
func checkPermission(role: UserRole, action: String, isOwner: Bool) -> ActionResult {
    switch role {
    case .banned:
        return .denied("Account is banned")
    case .admin:
        return .granted
    case .editor:
        if action == "delete" && !isOwner {
            return .denied("Only owners can delete")
        }
        return .granted
    case .viewer:
        switch (action, isOwner) {
        case (_, false):
            return .denied("Viewers cannot modify content")
        case ("read", true):
            return .granted
        default:
            return .denied("Unknown action")
        }
    case .guest:
        return action == "read" ? .granted : .denied("Guests can only read")
    }
}
let testCases = [
    (UserRole.admin, "delete", false),
    (UserRole.editor, "delete", true),
    (UserRole.editor, "delete", false),
    (UserRole.viewer, "read", true),
    (UserRole.viewer, "write", false),
    (UserRole.guest, "read", false),
    (UserRole.banned, "read", false)
]
for (role, action, isOwner) in testCases {
    let result = checkPermission(role: role, action: action, isOwner: isOwner)
    switch result {
    case .granted:
        print("[GRANTED] \(role) can \(action)")
    case .denied(let reason):
        print("[DENIED] \(role) cannot \(action) -- \(reason)")
    }
}

Output:

TEXT 📖 Display only
[GRANTED] admin can delete
[GRANTED] editor can delete
[DENIED] editor cannot delete -- Only owners can delete
[GRANTED] viewer can read
[DENIED] viewer cannot write -- Viewers cannot modify content
[GRANTED] guest can read
[DENIED] banned cannot read -- Account is banned

❓ FAQ

Q Why doesn't Swift's switch need break?
A Swift's switch automatically exits after matching a case and won't fall through to the next case. To intentionally fall through, you must explicitly write fallthrough.
Q How should I choose between if and switch?
A Use if for 2-3 branches. Use switch when you have more than 3 branches or need range/pattern matching. switch enforces exhaustive handling of all possibilities, making it safer than if.
Q What's the difference between ... and ..<?
A a...b includes both a and b (closed range). a..<b includes a but not b (half-open range). For example, 1...3 includes 1, 2, 3; 1..<3 includes 1, 2.
Q Does the ternary operator hurt code readability?
A For simple either-or choices (like assignments), the ternary is crisp and clear. When nesting ternary operators or when the logic gets complex, use if instead—a long one-liner becomes harder to read.
Q Can the default branch be omitted?
A No. Swift's switch must be exhaustive. If you've covered all cases (e.g. all cases of an enum), you can omit default.

📖 Summary


📝 Exercises

  1. Beginner: Use if/else to write a body temperature classifier: temp < 36.0 outputs "Low", 36.0...37.5 outputs "Normal", > 37.5 outputs "Fever".
  2. Intermediate: Refactor the beginner exercise using switch to also support temp < 35.0 outputting "Dangerously Low". Use range matching syntax.
  3. Challenge: Create a payment fee calculator. Fee rules: Credit card 2.5% (≥$100: 2.5% - 0.5%), Debit card 1.0% (capped at $5), PayPal 3.0% + $0.30. Implement with switch + where.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏