Bird
Raised Fist0
Kotlinprogramming~20 mins

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

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Kotlin Loop Control Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of break in nested loops
What is the output of this Kotlin code snippet?
Kotlin
fun main() {
    for (i in 1..3) {
        for (j in 1..3) {
            if (j == 2) break
            print("$i$j ")
        }
    }
}
A11 21 22 31 32
B11 21 31
C11 12 13 21 22 23 31 32 33
D11 12 21 22 31 32
Attempts:
2 left
💡 Hint
Remember that break exits the nearest enclosing loop.
❓ Predict Output
intermediate
2:00remaining
Effect of continue in a single loop
What will this Kotlin code print?
Kotlin
fun main() {
    for (i in 1..5) {
        if (i % 2 == 0) continue
        print(i)
    }
}
A135
B12345
C246
D1245
Attempts:
2 left
💡 Hint
continue skips the current iteration when the condition is true.
❓ Predict Output
advanced
2:00remaining
Output with labeled break in nested loops
What is the output of this Kotlin code?
Kotlin
fun main() {
    outer@ for (i in 1..3) {
        for (j in 1..2) {
            if (i * j > 2) break@outer
            print("$i$j ")
        }
    }
}
A11 12 21 22
B11 12 13 21 22 23 31 32 33
C11 12 21 22 31
D11 12 21
Attempts:
2 left
💡 Hint
The labeled break exits the outer loop when condition is met.
❓ Predict Output
advanced
2:00remaining
Continue with label in nested loops
What will this Kotlin code print?
Kotlin
fun main() {
    outer@ for (i in 1..3) {
        for (j in 1..3) {
            if (j == 2) continue@outer
            print("$i$j ")
        }
    }
}
A11 21 31
B11 12 21 22 31 32
C11 13 21 23 31 33
D11 13 22 23 32 33
Attempts:
2 left
💡 Hint
continue@outer skips to next iteration of outer loop when j == 2.
🧠 Conceptual
expert
2:00remaining
Behavior of break and continue in Kotlin loops
Which statement about break and continue in Kotlin is correct?
AUsing break or continue without labels always exits all nested loops.
BA break statement can only exit the innermost loop and cannot use labels.
CA continue statement skips the rest of the current iteration and can use labels to skip outer loops.
DLabels in Kotlin can only be used with break, not with continue.
Attempts:
2 left
💡 Hint
Think about how labels affect loop control in Kotlin.

Practice

(1/5)
1. What does the break statement do inside a Kotlin loop?
easy
A. Stops the entire loop immediately
B. Skips the current iteration and continues the loop
C. Restarts the loop from the beginning
D. Pauses the loop until a condition is met

Solution

  1. Step 1: Understand the role of break

    The break statement immediately stops the whole loop when executed.
  2. Step 2: Compare with other loop controls

    Unlike continue, which skips only the current step, break ends the loop fully.
  3. Final Answer:

    Stops the entire loop immediately -> Option A
  4. Quick Check:

    break stops loop = B [OK]
Hint: Remember: break = stop loop, continue = skip step [OK]
Common Mistakes:
  • Confusing break with continue
  • Thinking break skips only one iteration
  • Assuming break restarts the loop
2. Which of the following is the correct syntax to skip the current iteration in a Kotlin for loop?
easy
A. continue
B. break
C. skip
D. stop

Solution

  1. Step 1: Identify the keyword to skip iteration

    In Kotlin, continue is used to skip the current loop iteration and move to the next.
  2. Step 2: Check other options for correctness

    break stops the loop, skip and stop are not valid Kotlin keywords.
  3. Final Answer:

    continue -> Option A
  4. Quick Check:

    Skip iteration = continue [OK]
Hint: Use continue to skip steps inside loops [OK]
Common Mistakes:
  • Using break instead of continue
  • Using non-existent keywords like skip or stop
  • Confusing syntax with other languages
3. What will be the output of this Kotlin code?
for (i in 1..5) {
    if (i == 3) break
    print(i)
}
medium
A. Error
B. 12345
C. 345
D. 12

Solution

  1. Step 1: Analyze the loop and break condition

    The loop runs from 1 to 5. When i equals 3, break stops the loop immediately.
  2. Step 2: Determine printed values before break

    Values 1 and 2 are printed before hitting i == 3, then loop stops.
  3. Final Answer:

    12 -> Option D
  4. Quick Check:

    Loop stops at 3, prints 1 and 2 [OK]
Hint: Break stops loop before printing 3 [OK]
Common Mistakes:
  • Assuming 3 is printed before break
  • Thinking loop continues after break
  • Confusing break with continue
4. Find the error in this Kotlin code snippet:
for (i in 1..5) {
    if (i == 3) continue
    if (i == 6) break
    print(i)
}
medium
A. The loop will run infinitely
B. continue cannot be used inside for loops
C. The break condition is unreachable
D. Syntax error in if statements

Solution

  1. Step 1: Check loop range and conditions

    The loop runs from 1 to 5, so i == 6 never happens.
  2. Step 2: Analyze break condition reachability

    Since i never reaches 6, the break statement is never executed, making it unreachable code.
  3. Final Answer:

    The break condition is unreachable -> Option C
  4. Quick Check:

    Break at i=6 unreachable in 1..5 loop [OK]
Hint: Check loop range matches break condition [OK]
Common Mistakes:
  • Assuming break will run at i=6
  • Thinking continue is invalid in loops
  • Believing loop runs infinitely
5. Consider this Kotlin code:
var sum = 0
for (i in 1..10) {
    if (i % 2 == 0) continue
    if (sum > 10) break
    sum += i
}
print(sum)

What is the output?
hard
A. 25
B. 11
C. 9
D. 16

Solution

  1. Step 1: Understand loop behavior with continue and break

    The loop adds only odd numbers (because even numbers are skipped by continue). It stops when sum > 10.
  2. Step 2: Calculate sum step-by-step

    Odd numbers: 1,3,5,7,9
    i=1: sum=0 <=10, sum=1
    i=3: sum=1 <=10, sum=4
    i=5: sum=4 <=10, sum=9
    i=7: sum=9 <=10, sum=16
    i=9: sum=16 >10, break (no add 9).
  3. Final Answer:

    11 -> Option B
  4. Quick Check:

    Sum odd numbers until sum > 10 = 11 [OK]
Hint: Add odds, stop when sum exceeds 10 [OK]
Common Mistakes:
  • Adding even numbers mistakenly
  • Breaking before sum exceeds 10
  • Miscounting sum steps
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard