0
0
Swiftprogramming~10 mins

Break and continue behavior 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 stop the loop when i equals 3.

Swift
for i in 1...5 {
    if i == 3 {
        [1]
    }
    print(i)
}
Drag options to blanks, or click blank then click option'
Abreak
Bexit
Creturn
Dcontinue
Attempts:
3 left
💡 Hint
Common Mistakes
Using continue instead of break, which skips only the current iteration.
2fill in blank
medium

Complete the code to skip printing the number 3 but continue the loop.

Swift
for i in 1...5 {
    if i == 3 {
        [1]
    }
    print(i)
}
Drag options to blanks, or click blank then click option'
Abreak
Bexit
Ccontinue
Dreturn
Attempts:
3 left
💡 Hint
Common Mistakes
Using break instead of continue, which stops the loop entirely.
3fill in blank
hard

Fix the error in the loop to correctly skip the number 2 and stop at 4.

Swift
for i in 1...5 {
    if i == 2 {
        [1]
    }
    if i == 4 {
        break
    }
    print(i)
}
Drag options to blanks, or click blank then click option'
Acontinue
Bbreak
Creturn
Dexit
Attempts:
3 left
💡 Hint
Common Mistakes
Using break to skip 2, which stops the loop too early.
4fill in blank
hard

Fill both blanks to skip odd numbers and stop the loop when i is 8.

Swift
for i in 1...10 {
    if i [1] 2 != 0 {
        [2]
    }
    if i == 8 {
        break
    }
    print(i)
}
Drag options to blanks, or click blank then click option'
A%
Bcontinue
C==
Dbreak
Attempts:
3 left
💡 Hint
Common Mistakes
Using == instead of % to check odd numbers.
Using break instead of continue to skip odd numbers.
5fill in blank
hard

Fill all three blanks to print only even numbers less than 7.

Swift
for i in 1...10 {
    if i [1] 2 != 0 {
        [2]
    }
    if i [3] 7 {
        break
    }
    print(i)
}
Drag options to blanks, or click blank then click option'
A%
Bcontinue
C>=
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using < instead of >= to stop loop.
Using break instead of continue to skip odd numbers.