What if you could instantly stop or skip parts of your task list with just one word?
Why Break and continue behavior in Kotlin? - Purpose & Use Cases
Imagine you are sorting through a long list of tasks, and you want to stop as soon as you find a specific one or skip certain tasks without checking them fully.
Without break and continue, you would have to write extra checks everywhere, making your code long and confusing. You might accidentally check tasks you don't need or miss stopping early, wasting time and effort.
Break and continue let you control loops easily: break stops the loop immediately, and continue skips the current step and moves to the next. This keeps your code clean and efficient.
for (task in tasks) { if (task == target) { // stop searching } // more checks }
for (task in tasks) { if (task == target) break if (shouldSkip(task)) continue // process task }
You can quickly control loop flow to save time and avoid unnecessary work.
When searching for a friend's name in a contact list, break stops the search once found, and continue skips contacts you don't want to check.
Break stops a loop immediately.
Continue skips the current loop step.
Both make loops easier and faster to manage.
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
