0
0
Swiftprogramming~10 mins

Labeled statements for nested loops 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 break out of the outer loop using the label.

Swift
outerLoop: for i in 1...3 {
    for j in 1...3 {
        if i == 2 && j == 2 {
            [1]
        }
        print("i = \(i), j = \(j)")
    }
}
Drag options to blanks, or click blank then click option'
Acontinue outerLoop
Bbreak
Ccontinue
Dbreak outerLoop
Attempts:
3 left
💡 Hint
Common Mistakes
Using break without the label only exits the inner loop.
Using continue instead of break does not exit the loop.
2fill in blank
medium

Complete the code to continue the outer loop when the inner loop reaches j == 2.

Swift
outer: for i in 1...3 {
    for j in 1...3 {
        if j == 2 {
            [1]
        }
        print("i = \(i), j = \(j)")
    }
}
Drag options to blanks, or click blank then click option'
Acontinue outer
Bbreak outer
Cbreak
Dcontinue
Attempts:
3 left
💡 Hint
Common Mistakes
Using break instead of continue changes the loop behavior.
Using continue without label only affects the inner loop.
3fill in blank
hard

Fix the error in the code to correctly break out of the outer loop.

Swift
outerLoop: for x in 1...3 {
    for y in 1...3 {
        if x == 3 && y == 1 {
            [1]
        }
        print("x = \(x), y = \(y)")
    }
}
Drag options to blanks, or click blank then click option'
Abreak outerloop
Bbreak OuterLoop
Cbreak outerLoop
Dbreak
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect case for the label name.
Using break without label only breaks inner loop.
4fill in blank
hard

Fill the blank to continue the outer loop and skip the current inner loop iteration when j == 3.

Swift
outerLoop: for i in 1...3 {
    for j in 1...3 {
        if j == 3 {
            [1]
        }
        print("i = \(i), j = \(j)")
    }
}
Drag options to blanks, or click blank then click option'
Acontinue outerLoop
Bbreak
Ccontinue
Dbreak outerLoop
Attempts:
3 left
💡 Hint
Common Mistakes
Using break instead of continue changes the loop behavior.
Using continue without label only affects inner loop.
5fill in blank
hard

Fill all three blanks to break out of the outer loop when i == 2 and j == 3, and print a message after the loops.

Swift
outer: for i in 1...3 {
    for j in 1...3 {
        if i == [1] && j == [2] {
            [3]
        }
        print("i = \(i), j = \(j)")
    }
}
print("Exited loops")
Drag options to blanks, or click blank then click option'
A2
B3
Cbreak outer
Dcontinue outer
Attempts:
3 left
💡 Hint
Common Mistakes
Using continue instead of break changes loop behavior.
Using wrong label name or case.
Incorrect condition values.