Break and continue behavior in Kotlin - Time & Space Complexity
When we use break or continue in loops, it changes how many times the loop runs.
We want to see how these keywords affect the total work the program does as input grows.
Analyze the time complexity of the following code snippet.
fun findFirstEven(numbers: List<Int>): Int? {
for (num in numbers) {
if (num % 2 == 0) {
return num
}
}
return null
}
fun printOdds(numbers: List<Int>) {
for (num in numbers) {
if (num % 2 == 0) continue
println(num)
}
}
This code searches for the first even number and stops early, and also prints only odd numbers skipping evens.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through the list of numbers.
- How many times: For
findFirstEven, loop stops early when an even number is found; forprintOdds, loop runs through all elements but skips printing evens.
For findFirstEven, if the first even number is near the start, the loop runs few times; if none or last, it runs through all.
For printOdds, the loop always checks every number, but skips printing evens.
| Input Size (n) | Approx. Operations in findFirstEven | Approx. Operations in printOdds |
|---|---|---|
| 10 | Between 1 and 10 checks | 10 checks and some prints |
| 100 | Between 1 and 100 checks | 100 checks and some prints |
| 1000 | Between 1 and 1000 checks | 1000 checks and some prints |
Pattern observation: break can reduce work if condition met early; continue skips some steps but loop still runs fully.
Time Complexity: O(n)
This means the program's work grows roughly in direct proportion to the input size, even with break or continue.
[X] Wrong: "Using break always makes the loop run in constant time."
[OK] Correct: Sometimes the loop stops early, but if the condition is met late or never, the loop still runs through all items.
Understanding how break and continue affect loop time helps you explain your code clearly and reason about efficiency in real tasks.
What if we replaced break with a flag variable to stop processing after finding the first even number? How would the time complexity change?
Practice
break statement do inside a Kotlin loop?Solution
Step 1: Understand the role of
Thebreakbreakstatement immediately stops the whole loop when executed.Step 2: Compare with other loop controls
Unlikecontinue, which skips only the current step,breakends the loop fully.Final Answer:
Stops the entire loop immediately -> Option AQuick Check:
breakstops loop = B [OK]
- Confusing break with continue
- Thinking break skips only one iteration
- Assuming break restarts the loop
for loop?Solution
Step 1: Identify the keyword to skip iteration
In Kotlin,continueis used to skip the current loop iteration and move to the next.Step 2: Check other options for correctness
breakstops the loop,skipandstopare not valid Kotlin keywords.Final Answer:
continue -> Option AQuick Check:
Skip iteration = continue [OK]
- Using break instead of continue
- Using non-existent keywords like skip or stop
- Confusing syntax with other languages
for (i in 1..5) {
if (i == 3) break
print(i)
}Solution
Step 1: Analyze the loop and break condition
The loop runs from 1 to 5. Wheniequals 3,breakstops the loop immediately.Step 2: Determine printed values before break
Values 1 and 2 are printed before hittingi == 3, then loop stops.Final Answer:
12 -> Option DQuick Check:
Loop stops at 3, prints 1 and 2 [OK]
- Assuming 3 is printed before break
- Thinking loop continues after break
- Confusing break with continue
for (i in 1..5) {
if (i == 3) continue
if (i == 6) break
print(i)
}Solution
Step 1: Check loop range and conditions
The loop runs from 1 to 5, soi == 6never happens.Step 2: Analyze break condition reachability
Sinceinever reaches 6, thebreakstatement is never executed, making it unreachable code.Final Answer:
The break condition is unreachable -> Option CQuick Check:
Break at i=6 unreachable in 1..5 loop [OK]
- Assuming break will run at i=6
- Thinking continue is invalid in loops
- Believing loop runs infinitely
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?
Solution
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.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).Final Answer:
11 -> Option BQuick Check:
Sum odd numbers until sum > 10 = 11 [OK]
- Adding even numbers mistakenly
- Breaking before sum exceeds 10
- Miscounting sum steps
Ready to prove it?
Pass the test and this pattern moves unknown
