0
0
Swiftprogramming~10 mins

Repeat-while loop in Swift - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
Anumber <= 3
Bnumber < 1
Cnumber == 0
Dnumber > 3
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.
2fill in blank
medium

Complete 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'
Acount >= 5
Bcount > 5
Ccount == 5
Dcount < 5
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.
3fill in blank
hard

Fix 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'
Avalue > 10
Bvalue < 10
Cvalue <= 10
Dvalue == 10
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.
4fill in blank
hard

Fill 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'
A+=
B-=
C<=
D>=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-=' decreases the number, causing an infinite loop.
Using '>=' in the condition causes the loop to never run.
5fill in blank
hard

Fill 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'
A10
B-=
C>=
D+=
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.