What if you could tell your program to do a task for every item without writing it again and again?
Why For loop over collections in Kotlin? - Purpose & Use Cases
Imagine you have a list of your favorite songs and you want to print each song's name one by one. Doing this manually means writing a print statement for each song, which is tiring and boring.
Manually writing code for each item is slow and easy to mess up. If you add or remove songs, you must change your code every time. This wastes time and causes mistakes.
Using a for loop lets you tell the computer: "Go through each song in the list and do something." This way, no matter how many songs you have, one simple loop handles them all automatically.
println(songs[0]) println(songs[1]) println(songs[2])
for (song in songs) { println(song) }
For loops let you easily repeat actions for every item in a collection, saving time and avoiding errors.
Think about checking every email in your inbox to find important messages. A for loop helps you look at each email quickly without writing extra code for each one.
Manually handling each item is slow and error-prone.
For loops automate repeating tasks over collections.
This makes your code simpler, cleaner, and easier to update.