Kotlin is a statically typed programming language that runs on the Java Virtual Machine (JVM). It was developed by JetBrains and is now an official language for Android development. Kotlin is designed to be concise, expressive, and safe.
It is also interoperable with Java, which means that you can use Kotlin and Java code together in the same project.
Kotlin For Loop & Kotlin Ranges (Lesson 10 PDF)
Overview about the Lesson
In this lesson, we’ll cover the basics of Kotlin For Loop & Kotlin Ranges, including how to create and initialize ranges, how to access and modify range elements, and how to traverse ranges.



The Content Of The Lesson
The lesson is divided into the following sections:
- Creating and initializing ranges
- Accessing and modifying range elements
- Traversing ranges
- Using for loop with ranges
- Using while loop with ranges
- Using do-while loop with ranges
Why This Lesson
For Loop & Ranges are fundamental concepts in programming, and Kotlin provides excellent support for working with them. By the end of this lesson, you’ll have a solid understanding of Kotlin For Loop & Kotlin Ranges and how to use them in your programs.
Kotlin for Loops and Ranges: Examples
The PDF covers for loops and ranges. These examples show every common form, with the output you should see.
Ranges
| Expression | Values | Note |
|---|---|---|
| 1..5 | 1, 2, 3, 4, 5 | Inclusive at both ends |
| 1 until 5 | 1, 2, 3, 4 | Excludes the end (also written 1..<5) |
| 5 downTo 1 | 5, 4, 3, 2, 1 | Counts down |
| 1..10 step 3 | 1, 4, 7, 10 | Custom step |
| ‘a’..’e’ | a, b, c, d, e | Character ranges work too |
Check membership with in: if (age in 13..19) println("Teenager").
for loop forms
fun main() {
for (i in 1..3) print("$i ") // 1 2 3
println()
for (i in 10 downTo 0 step 5) print("$i ") // 10 5 0
println()
val colors = listOf("red", "green", "blue")
for (c in colors) println(c)
for ((index, c) in colors.withIndex()) {
println("$index -> $c") // 0 -> red, 1 -> green, 2 -> blue
}
val prices = mapOf("tea" to 2.5, "coffee" to 3.0)
for ((item, price) in prices) println("$item costs $price")
}Kotlin has no C-style for (int i = 0; i < n; i++). Ranges replace it: for (i in 0 until n).
repeat and forEach
fun main() {
repeat(3) { println("Hello #$it") } // it = 0, 1, 2
(1..3).forEach { print(it * it) } // 149
}repeat(n) is the simplest way to run something n times. Inside forEach, use return@forEach to skip to the next item.
Nested loops and labels
fun main() {
outer@ for (row in 1..3) {
for (col in 1..3) {
if (row * col == 4) break@outer // leaves both loops
print("${row * col} ")
}
}
}
// Output: 1 2 3 2A label (outer@) lets break or continue act on the outer loop instead of the inner one.
Practice exercise
Print the multiplication table of 7 from 7 × 1 to 7 × 10.
fun main() {
for (i in 1..10) {
println("7 x $i = ${7 * i}")
}
}Frequently Asked Questions
What is the difference between .. and until?
1..5 includes 5; 1 until 5 stops at 4. Use until (or ..<) when looping over indexes, e.g. for (i in 0 until list.size), or simply list.indices.
Can I loop backwards over a list?
Yes: for (i in list.indices.reversed()) or for (item in list.asReversed()).
Continue the course
← Previous: Lesson 9: Arrays | Next: Lesson 11: Functions & Classes →
- Lesson 1: Getting Started
- Lesson 2: Syntax & Output
- Lesson 3: Comments & Variables
- Lesson 4: Data Types
- Lesson 5: Operators
- Lesson 6: Set Operations & Grouping
- Lesson 7: Booleans & If…Else
- Lesson 8: When, While, Break & Continue
- Lesson 9: Arrays
- Lesson 10: For Loops & Ranges (you are here)
- Lesson 11: Functions & Classes
- Lesson 12: Classes, Objects & Constructors
- Lesson 13: Class Functions & Inheritance
Conclusion
In this blog post, we’ve covered the basics of Kotlin For Loop & Kotlin Ranges, including how to create and initialize ranges, how to access and modify range elements, and how to traverse ranges.
We’ve also explored some advanced topics, such as using for loop, while loop, and do-while loop with ranges. With this knowledge, you’ll be well-equipped to work with For Loop & Ranges in your Kotlin programs.
Download From Link
You can download the lesson from the following link:













