Chapter 7: Loops

Loops in Kotlin — this is the chapter where your programs start doing real work by repeating tasks efficiently and elegantly!

Kotlin has beautiful, clean, and very powerful loop constructs — much nicer than Java. We’ll cover everything super slowly, with real-life analogies, lots of copy-paste examples, step-by-step breakdowns, tables, common mistakes with fixes, and fun facts so it all sticks perfectly.

Grab your cutting chai ☕ — let’s dive in!

1. for Loop – The Most Beautiful Loop in Kotlin

Kotlin’s for loop is not like Java’s for (int i = 0; i < 10; i++) — it’s much more expressive and idiomatic.

Main forms of for loop:

Type Syntax Example Use When
Range for (i in 1..10) Known count (numbers)
Until for (i in 1 until 10) Exclude the end
DownTo for (i in 10 downTo 1) Counting backwards
Step for (i in 1..10 step 2) Jump by step
Array / Collection for (item in list) Iterate over elements
With index for ((index, value) in list.withIndex()) Need both index & value

A. Looping over ranges

Kotlin

Fun fact: You can use downTo with step — very useful for reverse countdowns!

B. Looping over arrays & collections

Kotlin

Output:

text

C. Looping over arrays with indices

Kotlin

2. while & do-while Loops

These are almost identical to Java — but cleaner.

while – check condition first

Kotlin

do-while – run at least once, then check condition

Kotlin

3. Labels – @loop, break@loop, continue@loop

Kotlin allows labels on loops → you can break or continue a specific outer loop.

Real-life analogy: You’re in a big mall with many floors (nested loops). Normal break only takes you out of the current floor. With label → you can say “break@mall” → exit the entire mall!

Example – Nested loops with labels

Kotlin

Output:

text

With continue@label

Kotlin

Output:

text

4. return@label in Lambdas

Very powerful when you want to return from a lambda (not just continue/break).

Example – Find first even number in nested loops

Kotlin

Output:

text

5. Quick Recap Table (Your Cheat Sheet)

Loop Type Syntax Example Key Feature
for range for (i in 1..10) Inclusive range
for until for (i in 1 until 10) Excludes end
for downTo for (i in 10 downTo 1) Countdown
for collection for (item in list) Clean for-each
for with index for ((i, v) in list.withIndex()) Get index + value
while while (condition) { … } Check first
do-while do { … } while (condition) Run at least once
break@label break@outer Exit specific loop
return@label return@loop Return from lambda block

6. Common Newbie Mistakes & Fixes

Mistake Problem Fix
Writing for (i = 0; i < 10; i++) Java style → compile error Use for (i in 0 until 10)
Forgetting in keyword for (i 1..10) → error Always write in
Using break in nested loops Only breaks inner loop Use break@label
return inside lambda Returns from lambda (not outer function) Use return@label
Confusing .. and until Off-by-one bugs Remember: .. includes end

7. Homework for You (Let’s Make It Fun!)

  1. Basic Print numbers 1 to 10 using for (i in 1..10)
  2. Medium Print even numbers from 1 to 20 using step 2
  3. Advanced Nested loop: print multiplication table 1 to 5 (1 × 1 = 1, 1 × 2 = 2, …, 5 × 5 = 25)
  4. Fun Ask user for a number N → print countdown from N to 1 using downTo
  5. Challenge Use labeled break to exit two nested loops when you find number 42 in a 5×5 grid.

You’ve just mastered Kotlin’s beautiful loops — now your programs can repeat tasks like a pro!

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *