0
0
Swiftprogramming~15 mins

Switch with where clauses in Swift - Mini Project: Build & Apply

Choose your learning style9 modes available
Switch with where clauses
📖 Scenario: You are building a simple program to categorize ages into life stages using Swift's switch statement with where clauses. This helps you understand how to add extra conditions to each case.
🎯 Goal: Create a Swift program that uses a switch statement with where clauses to print the life stage of a person based on their age.
📋 What You'll Learn
Create an age variable with the value 25
Create a switch statement on age
Use where clauses in case statements to check age ranges
Print the correct life stage for the given age
💡 Why This Matters
🌍 Real World
Using switch with where clauses helps you handle multiple conditions clearly, like categorizing ages, grades, or other ranges in apps.
💼 Career
Understanding switch with where clauses is useful for Swift developers building iOS apps that need clear, readable decision-making code.
Progress0 / 4 steps
1
Create the age variable
Create a variable called age and set it to 25.
Swift
Need a hint?

Use var age = 25 to create the variable.

2
Start the switch statement on age
Write a switch statement on the variable age.
Swift
Need a hint?

Use switch age { } to start the switch statement.

3
Add case statements with where clauses
Inside the switch on age, add these case statements with where clauses:
1. case let x where x < 13: print "Child"
2. case let x where x < 20: print "Teenager"
3. case let x where x < 65: print "Adult"
4. default: print "Senior"
Swift
Need a hint?

Use case let x where condition: to add conditions inside switch cases.

4
Run the program to print the life stage
Run the program so it prints the life stage for the age 25.
Swift
Need a hint?

Just run the program. It will print the life stage automatically.