Recall & Review
beginner
What is a labeled statement in Swift?
A labeled statement is a name given to a loop or block of code, allowing you to control flow explicitly, especially useful for nested loops.
Click to reveal answer
beginner
How do you define a label for a loop in Swift?
You write the label name followed by a colon before the loop. For example:<br>
outerLoop: for i in 1...3 { ... }Click to reveal answer
intermediate
Why use labeled statements with nested loops?
They let you break or continue an outer loop directly from inside an inner loop, making it easier to control complex loop flows.Click to reveal answer
beginner
What happens if you use 'break' without a label inside nested loops?
The 'break' will only exit the innermost loop, not the outer loops.
Click to reveal answer
intermediate
Example: How to break out of an outer loop named 'outerLoop' from inside an inner loop?
Use
break outerLoop inside the inner loop to exit the outer loop immediately.Click to reveal answer
How do you label a loop named 'mainLoop' in Swift?
✗ Incorrect
The correct syntax is to write the label followed by a colon before the loop.
What does 'break outerLoop' do inside nested loops?
✗ Incorrect
'break outerLoop' exits the loop that has the label 'outerLoop'.
If you want to skip the current iteration of an outer loop from inside an inner loop, what do you use?
✗ Incorrect
'continue outerLoop' skips the current iteration of the loop labeled 'outerLoop'.
What happens if you use 'break' without a label inside nested loops?
✗ Incorrect
'break' without a label exits only the innermost loop.
Which of these is a valid use of a labeled statement in Swift?
✗ Incorrect
Label must be before the loop, and 'break outer' correctly exits the labeled loop.
Explain how labeled statements help control flow in nested loops in Swift.
Think about how you can exit or skip outer loops from inside inner loops.
You got /4 concepts.
Write a simple Swift code example using a labeled statement to break out of an outer loop from inside an inner loop.
Use a label before the outer loop and 'break labelName' inside the inner loop.
You got /4 concepts.