Filtering User Ages with For-in and Where Clause
📖 Scenario: You are managing a simple database of users and their ages. You want to find users who are adults (18 years or older) to send them a special offer.
🎯 Goal: Build a Swift program that uses a for-in loop with a where clause to filter and collect users who are 18 or older.
📋 What You'll Learn
Create a dictionary called
users with these exact entries: "Alice": 17, "Bob": 20, "Charlie": 16, "Diana": 22Create an empty array of strings called
adultUsers to store names of adult usersUse a
for-in loop with a where clause to iterate over users and select only those with age 18 or olderInside the loop, append the user's name to
adultUsers💡 Why This Matters
🌍 Real World
Filtering data based on conditions is common in databases and apps, like selecting users eligible for offers.
💼 Career
Understanding loops with conditions helps in writing efficient queries and data processing code in many programming jobs.
Progress0 / 4 steps