For-in Loop with Collections in Swift
📖 Scenario: You work at a small bookstore. You have a list of book titles and their prices. You want to find out which books cost more than $20.
🎯 Goal: Build a Swift program that uses a for-in loop to check each book's price and collect the titles of books costing more than $20.
📋 What You'll Learn
Create a dictionary called
books with these exact entries: "Swift Programming": 25, "iOS Development": 18, "Data Structures": 30, "Algorithms": 15Create a variable called
priceThreshold and set it to 20Use a
for-in loop with variables title and price to iterate over booksInside the loop, check if
price is greater than priceThreshold and if so, add title to a list called expensiveBooksPrint the
expensiveBooks list at the end💡 Why This Matters
🌍 Real World
This project shows how to filter items from a collection based on conditions, a common task in apps like shopping, inventory, or data analysis.
💼 Career
Understanding how to loop through collections and apply conditions is essential for software developers working with data structures and user data.
Progress0 / 4 steps