Recall & Review
beginner
What is a
for-in loop in Swift?A
for-in loop in Swift repeats a block of code for each item in a sequence, like numbers or arrays.Click to reveal answer
beginner
How do you create a range from 1 to 5 in Swift?
You write
1...5 to create a range including numbers 1 through 5.Click to reveal answer
intermediate
What is the difference between
1...5 and 1..<5 in Swift?1...5 includes 1, 2, 3, 4, 5. 1..<5 includes 1, 2, 3, 4 but not 5.Click to reveal answer
beginner
Write a simple Swift
for-in loop that prints numbers 1 to 3.for number in 1...3 {
print(number)
}
This prints 1, then 2, then 3 each on a new line.
Click to reveal answer
intermediate
Can you use a
for-in loop with ranges to count backwards in Swift?Yes! Use
stride(from:to:by:) or stride(from:through:by:) to count backwards with a for-in loop.Click to reveal answer
What does the range
1...5 include in Swift?✗ Incorrect
1...5 is a closed range including all numbers from 1 to 5.Which Swift syntax counts from 1 up to but not including 5?
✗ Incorrect
1..<5 is a half-open range that excludes 5.How do you write a
for-in loop to print numbers 1 to 3 in Swift?✗ Incorrect
Use
1...3 to include 3, and the for-in syntax is correct.Which method helps count backwards in a
for-in loop with ranges?✗ Incorrect
stride(from:to:by:) lets you specify steps, including negative steps to count backwards.What will this code print?
for i in 1..<4 {
print(i)
}
✗ Incorrect
1..<4 includes 1, 2, 3 but not 4.Explain how to use a for-in loop with a range in Swift to repeat an action multiple times.
Think about how to write a loop that counts from 1 to 5.
You got /3 concepts.
Describe the difference between closed and half-open ranges in Swift and how they affect for-in loops.
Focus on whether the last number is included or not.
You got /4 concepts.