0
0
Swiftprogramming~15 mins

Break and continue behavior in Swift - Mini Project: Build & Apply

Choose your learning style9 modes available
Break and continue behavior
📖 Scenario: Imagine you are organizing a small party and you have a list of guests with their ages. You want to invite only adults (age 18 and above) but stop checking guests once you find a guest who is under 10 years old, as they are too young for the party.
🎯 Goal: You will write a Swift program that uses break and continue statements to process the guest list. You will skip guests who are under 18 but continue checking others, and stop the process entirely if a guest is under 10.
📋 What You'll Learn
Create a dictionary called guests with these exact entries: "Alice": 25, "Bob": 17, "Charlie": 9, "Diana": 30, "Eve": 20
Create an empty array called invitedGuests to store names of guests who are invited
Use a for loop with variables name and age to iterate over guests
Inside the loop, use continue to skip guests younger than 18
Inside the loop, use break to stop the loop if a guest is younger than 10
Add the guest's name to invitedGuests if they are 18 or older
Print the invitedGuests array at the end
💡 Why This Matters
🌍 Real World
This project simulates filtering a list of people based on age criteria, which is common in event planning, marketing, or user management.
💼 Career
Understanding break and continue is important for controlling loops efficiently in many programming tasks, such as data processing, filtering, and early exit conditions.
Progress0 / 4 steps
1
Create the guest list dictionary
Create a dictionary called guests with these exact entries: "Alice": 25, "Bob": 17, "Charlie": 9, "Diana": 30, "Eve": 20
Swift
Need a hint?

Use square brackets [] to create a dictionary with key-value pairs separated by colons.

2
Create the invited guests array
Create an empty array called invitedGuests to store names of guests who are invited
Swift
Need a hint?

Use var to create a variable array of type [String] and set it to empty [].

3
Use a for loop with break and continue
Use a for loop with variables name and age to iterate over guests. Inside the loop, use continue to skip guests younger than 18. Use break to stop the loop if a guest is younger than 10. Add the guest's name to invitedGuests if they are 18 or older.
Swift
Need a hint?

Remember, break stops the whole loop, continue skips to the next item.

4
Print the invited guests
Write print(invitedGuests) to display the list of invited guests
Swift
Need a hint?

The output should show the invited guests array with only the names of guests 18 or older before the break.