"Testing1"
Kotlin Loops
Kotlin Loops
Loops help you repeat tasks automatically, saving you from writing the same line multiple times.
for loop
Used to go through ranges, arrays, or collections.
for (number in 1..5) {
println(number)
}
Goes from 1 to 5, one by one.
while loop
Runs instructions repeatedly while the stated rule stays valid
var count = 1
while (count <= 3) {
println("Repeat $count")
count++
}
Keeps printing until count becomes 4.
do while loop
Performs the block first, checks condition later.
var index = 1
do {
println("Step $index")
Index++ } while (index <= 2)
Loop Control
You can control loop flow using:
- break → Stops the loop.
- continue → Skips to the next round.
for (i in 1..5) {
if (i == 3) continue
println(i)
}
Skips number 3 and prints others.
Prefer Learning by Watching?
Watch these YouTube tutorials to understand KOTLIN Tutorial visually:
What You'll Learn:
- 📌 Loops In Kotlin Explained | Kotlin Loops Tutorial | Kotlin Tutorial For Beginners | Simplilearn
- 📌 #15 Kotlin Tutorial Loops and Range