Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to start a repeat-while loop that prints numbers from 1 to 3.
Swift
var number = 1 repeat { print(number) number += 1 } while [1]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a condition that never becomes false, causing an infinite loop.
Using a condition that is false at the start, so the loop never runs.
✗ Incorrect
The repeat-while loop continues as long as the condition 'number <= 3' is true, printing numbers 1 to 3.
2fill in blank
mediumComplete the code to repeat the loop while the variable 'count' is less than 5.
Swift
var count = 0 repeat { print("Count is \(count)") count += 1 } while [1]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' instead of '<', which would cause the loop to never run.
Using '==' which only runs if count is exactly 5.
✗ Incorrect
The loop repeats while 'count' is less than 5, printing counts from 0 to 4.
3fill in blank
hardFix the error in the repeat-while loop condition to stop when 'value' reaches 10.
Swift
var value = 2 repeat { print(value) value += 2 } while [1]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' excludes the value 10 from the loop output.
Using '==' only runs the loop if value is exactly 10.
✗ Incorrect
The loop should continue while 'value' is less than or equal to 10 to include 10 in the output.
4fill in blank
hardFill both blanks to create a repeat-while loop that prints even numbers from 2 to 8.
Swift
var num = 2 repeat { print(num) num [1] 2 } while num [2] 8
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-=' decreases the number, causing an infinite loop.
Using '>=' in the condition causes the loop to never run.
✗ Incorrect
The loop adds 2 to 'num' each time and continues while 'num' is less than or equal to 8.
5fill in blank
hardFill all three blanks to create a repeat-while loop that prints numbers from 10 down to 2, decreasing by 2.
Swift
var n = [1] repeat { print(n) n [2] 2 } while n [3] 2
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+=' instead of '-=' causes the loop to increase and never stop.
Using '<=' in the condition causes the loop to never run.
✗ Incorrect
The loop starts at 10, subtracts 2 each time, and continues while n is greater than or equal to 2.