Kotlin Break and Continue
Kotlin break and continue
In Kotlin, break and continue are jump statements used inside loops to control the flow of execution.
1. break Statement
The break statement is used to terminate (stop) the loop immediately.
Example: break in while Loop
Output:
Example: break in for Loop
2. continue Statement
The continue statement is used to skip the current iteration and move to the next loop cycle.
Example: continue in while Loop
Output:
Example: continue in for Loop
3. Labeled break and continue
Kotlin allows labels to control nested loops.
Labeled break
Labeled continue
4. When to Use break and continue
-
Use
breakwhen you want to exit the loop early -
Use
continuewhen you want to skip certain values -
Use labels for nested loops
Summary
-
break→ stops the loop -
continue→ skips current iteration -
Labels help control nested loops
-
Works with
for,while, anddo-while
