0
0
Swiftprogramming~20 mins

Break and continue behavior in Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Swift Loop Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of break in a loop
What is the output of this Swift code?
Swift
var sum = 0
for i in 1...5 {
    if i == 3 {
        break
    }
    sum += i
}
print(sum)
A10
B6
C15
D3
Attempts:
2 left
💡 Hint
Remember, break stops the loop immediately.
Predict Output
intermediate
2:00remaining
Output of continue in a loop
What is the output of this Swift code?
Swift
var sum = 0
for i in 1...5 {
    if i == 3 {
        continue
    }
    sum += i
}
print(sum)
A15
B10
C12
D9
Attempts:
2 left
💡 Hint
Continue skips the current loop iteration.
🔧 Debug
advanced
2:30remaining
Why does this break not stop the outer loop?
Consider this Swift code with nested loops. What is the output and why?
Swift
outerLoop: for i in 1...3 {
    for j in 1...3 {
        if j == 2 {
            break
        }
        print("i:\(i), j:\(j)")
    }
}
A
i:1, j:1
i:2, j:1
i:3, j:1
B
i:1, j:1
i:1, j:2
i:2, j:1
i:2, j:2
i:3, j:1
i:3, j:2
Ci:1, j:1
D
i:1, j:1
i:2, j:1
Attempts:
2 left
💡 Hint
Break without label stops only the inner loop.
Predict Output
advanced
2:30remaining
Output with labeled continue
What is the output of this Swift code using labeled continue?
Swift
outer: for i in 1...3 {
    for j in 1...3 {
        if j == 2 {
            continue outer
        }
        print("i:\(i), j:\(j)")
    }
}
Ai:1, j:1
B
i:1, j:1
i:2, j:1
i:3, j:1
C
i:1, j:1
i:1, j:3
i:2, j:1
i:2, j:3
i:3, j:1
i:3, j:3
D
i:1, j:1
i:1, j:2
i:2, j:1
i:2, j:2
i:3, j:1
i:3, j:2
Attempts:
2 left
💡 Hint
Labeled continue skips to next iteration of outer loop.
🧠 Conceptual
expert
3:00remaining
Effect of break in switch inside loop
What is the output of this Swift code?
Swift
for i in 1...3 {
    switch i {
    case 2:
        break
    default:
        print(i)
    }
}
A
1
3
B
1
2
3
C2
D1
Attempts:
2 left
💡 Hint
Break inside switch exits switch, not the loop.