Complete the code to print numbers from 1 to 5.
for number in [1] { print(number) }
The correct syntax for a closed range in Swift is 1...5, which includes both 1 and 5.
Complete the code to print numbers from 1 up to but not including 5.
for number in [1] { print(number) }
... which includes the end value.The half-open range operator ..< includes the start but excludes the end, so 1..<5 prints 1, 2, 3, 4.
Fix the error in the code to print numbers from 1 to 3.
for i in [1] { print(i) }
The correct closed range operator in Swift is ..., so 1...3 includes 1, 2, and 3.
Fill both blanks to create a loop that prints even numbers from 2 to 10.
for number in [1] { if number [2] 2 == 0 { print(number) } }
The range 2...10 includes numbers 2 through 10. The modulo operator % checks if the number is divisible by 2 (even).
Fill all three blanks to create a dictionary of numbers and their squares for numbers 1 to 5, but only include squares greater than 10.
var squares = [Int: Int]() for number in [3] { if number * number > 10 { squares[[1]] = [2] } }
Declare var squares as an empty dictionary of type [Int: Int]. Loop over the closed range 1...5. For each number where number * number > 10, assign squares[number] = number * number.