Loops (iterative statements): for and while loop; break, continue and return

Kotlin's for loop iterates over ranges and collections (for item in list), while loops on a condition, and break, continue and return control the flow, with labels to target outer loops.

10 min read · 9 cards · 2 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

Doing something to every item

FestConnect Mobile has its list of registrations; now it must PROCESS them: print each, count the paid ones, find a specific name. That means LOOPING.

Kotlin's loops will feel familiar from Java, with one notable change: there is NO C-style for (i = 0; i < n; i++) loop. Instead, Kotlin's for always iterates over something ITERABLE, a range or a collection, which reads more clearly. This short lesson covers Kotlin's for and while loops and the control keywords break, continue and return, completing the language basics before the OOP unit.

Theory

for iterates over ranges and collections

Kotlin's for loop always walks over something iterable:

  • a range: for (i in 1..5) { ... } (1 to 5)
  • a collection: for (event in events) { ... } (each item directly)
  • with indices: for (i in events.indices) or for ((i, e) in events.withIndex()) (index and item)

There is NO for (i = 0; i < n; i++); Kotlin uses ranges (for (i in 0 until events.size)) instead. The for (event in events) form is the everyday one: it hands you each element, no index bookkeeping. while and do-while work exactly as in Java: loop while a condition holds.

Practical

for over a list, with break and continue

fun main() {
    val events = listOf("Garba", "Coding", "Robo", "Quiz")

    // for over a collection: each item directly
    for (event in events) {
        if (event == "Robo") continue   // skip Robo
        if (event == "Quiz") break      // stop at Quiz
        println(event)                  // Garba, Coding
    }

    // for over a range with index
    for (i in 1..3) {
        println("Pass $i")              // Pass 1, Pass 2, Pass 3
    }

    // while: condition-controlled, as in Java
    var seats = 3
    while (seats > 0) {
        seats--
    }
    println(seats)                     // 0
}

Theory

break, continue, return, and labels

Three control keywords steer loops:

  • break: exit the loop entirely
  • continue: skip to the next iteration
  • return: exit the whole FUNCTION (not just the loop)

In the listing, continue skips Robo and break stops at Quiz, so only Garba and Coding print.

For NESTED loops, plain break/continue affect only the INNERMOST loop. To target an OUTER loop, use a label: write outer@ before the loop, then break@outer or continue@outer jumps to that labelled loop. This is the same nested-loop escape you met in Dart (BCA405-02) and Java (BCA403), with Kotlin's @ label syntax.

Quiz

What does Kotlin NOT have, that Java does, for looping?

  1. The while loop
  2. The C-style for loop: for (i = 0; i < n; i++); Kotlin uses ranges (for i in 0 until n) or collection iteration instead
  3. The break keyword
  4. The ability to loop at all
Show the answer

The C-style for loop: for (i = 0; i < n; i++); Kotlin uses ranges (for i in 0 until n) or collection iteration instead

Kotlin deliberately has NO C-style for loop (for (i = 0; i < n; i++)). Instead, its for always iterates over something iterable: a range (for i in 1..5 or for i in 0 until n), a collection (for item in list), or indices. This reads more clearly and avoids off-by-one and infinite-loop bugs common with manual counters. Option A is wrong: Kotlin HAS while (unchanged from Java). Option C is wrong: break exists. Option D is absurd. So the one Java looping construct absent in Kotlin is the manual-counter for; you express the same thing with ranges, which is more concise and less error-prone. for (item in collection) is the everyday form.

Think first

Why drop the C-style for loop?

Java's for (i = 0; i < n; i++) is familiar and flexible. Why did Kotlin remove it in favour of ranges and collection iteration? Then tap.

Show the answer

Because the C-style for loop is a frequent source of BUGS and boilerplate: off-by-one errors (< vs <=), forgetting to increment (infinite loop), or getting the counter logic subtly wrong, all of which you met in earlier languages. Kotlin's ranges (for i in 0 until n) and direct collection iteration (for item in list) express the SAME intent more clearly and safely: no manual counter to mismanage, no bounds to fumble. When you just want each item, for (item in list) is unbeatable, no index at all. When you need an index, for (i in list.indices) is explicit and correct by construction. Removing the manual for is part of Kotlin's philosophy: prefer safer, more expressive constructs over flexible-but-error-prone ones. You lose nothing (ranges cover every case) and gain readability and fewer bugs.

Watch out

Loop traps

Looking for the C-style for: use ranges (for i in 0 until n) or for (item in list) instead.

.. vs until in loops: 1..n includes n; 0 until n stops at n-1 (right for 0-based indices).

break/continue in nested loops: they affect only the inner loop; use a label (break@outer) for the outer one.

return exits the whole function: not just the loop; use break to leave a loop.

Infinite while: ensure the condition eventually becomes false (change the variable inside).

Theory

Kotlin basics complete; now OOP

You now have Kotlin's fundamentals: variables and null safety, conditionals with when and ranges, collections, and loops, enough to write real logic. Unit 2 builds FestConnect Mobile's structure with Kotlin's OBJECT-ORIENTED features: classes, constructors, named parameters, and inheritance with its own twist (classes are final by default; you must mark them open to inherit). Your Java OOP knowledge transfers directly, with Kotlin's concise syntax. From fundamentals to structure.

Summary

Key takeaways

  • Kotlin's for always iterates over something iterable: a range (for i in 1..5), a collection (for item in list), or indices.
  • There is NO C-style for (i=0; i<n; i++); use ranges (for i in 0 until n) instead.
  • while and do-while work as in Java (condition-controlled).
  • break exits the loop, continue skips to the next iteration, return exits the whole function.
  • For nested loops, labels (outer@ ... break@outer) target a specific outer loop.
  • for (item in collection) is the everyday form, handing you each element with no index.
  • Memory hook: Kotlin loops over ranges and collections, no manual counter for.

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Introduction to Kotlin

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

Loops (iterative statements): for and while loop; break, continue and return · Advance Mobile Technology - I (Major-11-02) · Gri-Learn