Introduction
Kotlin provides three loop constructs: for, while, and do-while. The for loop is designed to iterate over anything that provides an iterator - collections, arrays, or ranges - rather than the traditional C-style for (int i=0; i<n; i++) found in Java. Instead, Kotlin uses ranges like 1..5 to express counted iteration. while and do-while behave much like in other languages, repeating a block as long as a condition holds true.
Cricket analogy: Like a bowler running through overs using a fixed sequence of deliveries (a range like 1..5) rather than manually counting balls one by one with an index variable, Kotlin's for loop iterates over ranges instead of Java's C-style index counting.
Syntax
// for loop over a range
for (i in 1..5) {
// uses i
}
// for loop over a collection
for (item in collection) {
// uses item
}
// while loop
while (condition) {
// repeats while condition is true
}
// do-while loop
do {
// runs at least once
} while (condition)Explanation
Kotlin's for loop always uses the in keyword to iterate: for (i in 1..5) iterates over a range, and for (item in list) iterates over any collection or array. There is no direct equivalent of Java's index-based for (int i=0; i<n; i++); ranges and step expressions cover that use case instead. while checks its condition before each iteration, so the body may run zero times, whereas do-while always executes its body at least once before checking the condition. break exits the nearest enclosing loop entirely, and continue skips to the next iteration. For nested loops, labels (e.g. outer@ for (...) { ... break@outer }) let break or continue target a specific outer loop instead of just the innermost one.
Cricket analogy: Like an umpire who checks the light meter before every over starts (while), versus one who always lets the first over of a session happen before checking conditions (do-while), and can call off just the current match without ending the whole tournament (break with a label targeting one match).
Example
fun main() {
// for loop with a range
for (i in 1..5) {
print("$i ")
}
println()
// while loop
var count = 0
while (count < 3) {
println("count = $count")
count++
}
// labeled nested loop with break
outer@ for (i in 1..3) {
for (j in 1..3) {
if (j == 2) break@outer
println("i=$i, j=$j")
}
}
}
// Output:
// 1 2 3 4 5
// count = 0
// count = 1
// count = 2
// i=1, j=1Key Takeaways
- Kotlin's for loop iterates using
inover ranges, collections, or arrays - not C-style indexing. - while checks its condition before running the body, so it may execute zero times.
- do-while always executes its body at least once, checking the condition afterward.
- break exits the nearest loop, continue skips to the next iteration.
- Labels (e.g.
outer@) let break/continue target a specific loop in nested loop structures.
Practice what you learned
1. Which loop syntax is used for counted iteration in Kotlin, instead of a C-style `for (int i=0; i<n; i++)`?
2. What is the key difference between while and do-while loops?
3. What does the `break` keyword do inside a loop?
4. What is the purpose of a label like `outer@` on a loop?
5. In the example, why does the output stop after 'i=1, j=1'?
Was this page helpful?
You May Also Like
Ranges in Kotlin
Learn how Kotlin ranges express sequences of values for iteration and containment checks using .., until, downTo, and step.
if-else Expressions in Kotlin
Learn how Kotlin's if-else works as both a statement and a value-returning expression, replacing the ternary operator.
Collections in Kotlin
An overview of how Kotlin groups objects into Lists, Sets, and Maps using its dual mutable/read-only collection model.
Sequences in Kotlin
How Kotlin Sequences provide lazy, element-by-element evaluation of collection operations for better performance on large chains.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics