For-in loop with ranges
📖 Scenario: You are helping a small bakery count the number of cupcakes they bake each day for a week. They want to know the total cupcakes baked from Monday to Sunday.
🎯 Goal: Build a Swift program that uses a for-in loop with a range to sum the cupcakes baked each day and print the total.
📋 What You'll Learn
Create a dictionary called
cupcakesBaked with keys as days of the week and values as the number of cupcakes baked.Create a constant
days that holds the range from 1 to 7 representing days Monday to Sunday.Use a
for-in loop with the variable day to iterate over days.Inside the loop, add the cupcakes baked for each day to a variable
totalCupcakes.Print the total cupcakes baked after the loop.
💡 Why This Matters
🌍 Real World
Counting daily production or sales is common in small businesses to track performance and plan inventory.
💼 Career
Using loops and ranges to process data collections is a fundamental skill for software developers, especially in data processing and automation tasks.
Progress0 / 4 steps