📖 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
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
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
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
Hint
The output should show the invited guests array with only the names of guests 18 or older before the break.
Practice
(1/5)
1. What does the break statement do inside a Swift loop?
easy
A. Restarts the loop from the beginning.
B. Skips the current iteration and continues with the next one.
C. Pauses the loop until a condition is met.
D. Stops the entire loop immediately and exits it.
Solution
Step 1: Understand the role of break in loops
The break statement immediately stops the loop and exits it, no matter where it is in the loop.
Step 2: Compare with other options
continue skips to the next iteration, but break stops the loop entirely. Restarting or pausing the loop is not the behavior of break.
Final Answer:
Stops the entire loop immediately and exits it. -> Option D
Quick Check:
break stops loop [OK]
Hint: Break means stop the loop completely [OK]
Common Mistakes:
Confusing break with continue
Thinking break restarts the loop
Assuming break pauses the loop
2. Which of the following is the correct syntax to skip the current iteration in a Swift for loop?
easy
A. skip
B. continue
C. break
D. stop
Solution
Step 1: Identify the keyword to skip iteration
In Swift, continue is used to skip the rest of the current loop iteration and move to the next one.
Step 2: Verify other options
break stops the loop, skip and stop are not valid Swift keywords for loops.
Final Answer:
continue -> Option B
Quick Check:
Skip iteration = continue [OK]
Hint: Use continue to skip current loop step [OK]
Common Mistakes:
Using break instead of continue
Using non-existent keywords like skip or stop
Confusing continue with break
3. What will be the output of this Swift code?
for i in 1...5 {
if i == 3 {
break
}
print(i)
}
medium
A. 1 2
B. No output
C. 3 4 5
D. 1 2 3 4 5
Solution
Step 1: Analyze the loop and break condition
The loop runs from 1 to 5. When i equals 3, break stops the loop immediately.
Step 2: Determine printed values before break
Values 1 and 2 are printed before reaching 3. At 3, loop stops, so 3, 4, 5 are not printed.
Final Answer:
1 2 -> Option A
Quick Check:
Break stops at 3, prints before it [OK]
Hint: Break stops loop before printing 3 [OK]
Common Mistakes:
Assuming break skips only current iteration
Thinking all numbers print
Confusing break with continue
4. Find the error in this Swift code snippet:
for i in 1...5 {
if i == 3 {
continue
}
if i == 6 {
break
}
print(i)
}
medium
A. break should come before continue in the code.
B. continue cannot be used inside a for loop.
C. The loop will never break because i never reaches 6.
D. The range 1...5 is invalid.
Solution
Step 1: Check the loop range and conditions
The loop runs from 1 to 5, so i will never be 6.
Step 2: Analyze the break condition
Since i == 6 never occurs, the break statement will never execute, so the loop runs fully except skipping 3 due to continue.
Final Answer:
The loop will never break because i never reaches 6. -> Option C
Quick Check:
Break condition unreachable [OK]
Hint: Check if break condition can ever be true [OK]
Common Mistakes:
Thinking continue is invalid in for loops
Assuming break runs before continue always
Not checking loop range carefully
5. You want to print all numbers from 1 to 10 except 5 and stop printing after 8. Which code snippet correctly uses break and continue in Swift?
hard
A. for i in 1...10 {
if i == 5 { continue }
if i > 8 { break }
print(i)
}
B. for i in 1...10 {
if i == 5 { break }
if i > 8 { continue }
print(i)
}
C. for i in 1...10 {
if i == 5 { continue }
if i > 7 { continue }
print(i)
}
D. for i in 1...10 {
if i == 5 { break }
if i > 8 { break }
print(i)
}
Solution
Step 1: Understand requirements
Skip printing 5 (use continue) and stop printing after 8 (use break when > 8).
Step 2: Check each option
for i in 1...10 {
if i == 5 { continue }
if i > 8 { break }
print(i)
} uses continue to skip 5 and break to stop after 8, matching requirements exactly.
Final Answer:
for i in 1...10 {
if i == 5 { continue }
if i > 8 { break }
print(i)
} -> Option A
Quick Check:
Continue skips 5, break stops loop after 8 [OK]
Hint: Continue skips, break stops loop [OK]
Common Mistakes:
Using break to skip 5 instead of continue
Using continue to stop loop after 8
Mixing order of break and continue incorrectly
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.