0Pricing
Swift Academy · Lesson

Value Binding and where Clauses

Bind matched values and add extra conditions.

Value Binding and where Clauses is a free Swift Academy lesson on CoddyKit — lesson 3 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.

Binding in a Case

A switch case can bind the matched value to a constant with case let x, making the value usable inside that case.

let value = 42
switch value {
case let n:
    print("The number is \(n)")
}

Binding Part of a Tuple

You can bind just the parts of a tuple you need, using let on specific positions.

let point = (3, 7)
switch point {
case (let x, 0):
    print("On x-axis at \(x)")
case (0, let y):
    print("On y-axis at \(y)")
case let (x, y):
    print("At \(x), \(y)")
}

The where Clause

Add a where clause to a case to apply an extra Boolean condition on the bound value. The case matches only if both pattern and condition hold.

let number = 8
switch number {
case let n where n % 2 == 0:
    print("\(n) is even")
case let n:
    print("\(n) is odd")
}

where on Ranges

You can combine a range pattern with a where clause for finer control.

let score = 95
switch score {
case 90...100 where score == 100:
    print("Perfect")
case 90...100:
    print("Excellent")
default:
    print("Keep trying")
}

where With Tuples

A where clause can compare bound tuple elements to each other.

let point = (4, 4)
switch point {
case let (x, y) where x == y:
    print("On the diagonal")
case let (x, y):
    print("Off diagonal at \(x), \(y)")
}

Multiple where Conditions

The condition after where can be any Boolean expression, including && and ||.

let n = 15
switch n {
case let x where x > 10 && x < 20:
    print("In the teens-ish range")
default:
    print("Outside range")
}

Order With where

A case with a where clause only matches when the condition is true; otherwise Swift falls to the next case.

let temp = 5
switch temp {
case let t where t < 0:
    print("Freezing")
case let t where t < 10:
    print("Cold")
default:
    print("Mild or warm")
}

Binding in Range Cases

You can bind a value and still constrain it to a range using where.

let age = 67
switch age {
case let a where a >= 65:
    print("Senior, age \(a)")
case let a where a >= 18:
    print("Adult, age \(a)")
default:
    print("Minor")
}

Classifying With Bindings

Combining binding and where lets a single switch both categorize and report the value.

let balance = -50
switch balance {
case let b where b < 0:
    print("Overdrawn by \(-b)")
case 0:
    print("Empty")
case let b:
    print("Balance: \(b)")
}

Why Bindings Help

Without binding, you would re-reference the original variable. Binding gives a clean local name and pairs naturally with where filters.

let input = 21
switch input {
case let n where n.isMultiple(of: 3):
    print("\(n) is divisible by 3")
default:
    print("Not divisible by 3")
}

A Catch-All Binding

A final case let x with no condition acts like a default while still giving you the value.

let code = 17
switch code {
case 0:
    print("zero")
case let other:
    print("non-zero: \(other)")
}

Quick Check

Test value binding and where.

Recap: Value Binding and where Clauses

Use case let x to bind the matched value and add where to apply an extra condition. Together they let one switch categorize values and use them in the same case.

let n = 6
switch n {
case let x where x > 0: print("positive \(x)")
default: print("non-positive")
}

Frequently asked questions

Is the “Value Binding and where Clauses” lesson free?

Yes — the full text of “Value Binding and where Clauses” 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 “Value Binding and where Clauses”?

Bind matched values and add extra conditions. 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 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Value Binding and where Clauses” 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

  1. Exhaustive Switch on Values
  2. Matching Ranges and Tuples
  3. Value Binding and where Clauses
  4. Compound Cases and Fallthrough
← Back to Swift Academy