Kotlin tutorial for Beginners – Kotlin for Loop

Kotlin for Loop

The for loop in Kotlin iterates through anything that provides an iterator. In this article, you learn to create for loop (with the help of examples).

There is no traditional for loop in Kotlin unlike Java and other languages.

In Kotlin, for loop is used to iterate through ranges, arrays, maps and so on (anything that provides an iterator).

The syntax of for loop in Kotlin is:

for (item in collection) {
    // body of loop
}

Example: Iterate Through a Range

fun main(args: Array<String>) {

    for (i in 1..5) {
        println(i)
    }
}

Here, the loop iterates through the range and prints individual item.

When you run the program, the output will be:

1
2
3
4
5

If the body of the loop contains only one statement (like above example), it’s not necessary to use curly braces { }.

fun main(args: Array<String>) {
    for (i in 1..5) println(i)
}

It’s possible to iterate through a range using for loop because ranges provides an iterator. To learn more, visit Kotlin iterators.


Example: Different Ways to Iterate Through a Range


fun main(args: Array<String>) {

    print("for (i in 1..5) print(i) = ")
    for (i in 1..5) print(i)

    println()

    print("for (i in 5..1) print(i) = ")
    for (i in 5..1) print(i)             // prints nothing

    println()

    print("for (i in 5 downTo 1) print(i) = ")
    for (i in 5 downTo 1) print(i)

    println()

    print("for (i in 1..4 step 2) print(i) = ")
    for (i in 1..5 step 2) print(i)

    println()

    print("for (i in 4 downTo 1 step 2) print(i) = ")
    for (i in 5 downTo 1 step 2) print(i)
}

When you run the program, the output will be:

for (i in 1..5) print(i) = 12345
for (i in 5..1) print(i) = 
for (i in 5 downTo 1) print(i) = 54321
for (i in 1..4 step 2) print(i) = 135
for (i in 4 downTo 1 step 2) print(i) = 531

Iterating Through an Array

Here’s an example to iterate through a String array.

fun main(args: Array<String>) {

    var language = arrayOf("Ruby", "Koltin", "Python" "Java")

    for (item in language)
        println(item)
}

When you run the program, the output will be:

Ruby
Koltin
Python
Java

It’s possible to iterate through an array with an index. For example,

fun main(args: Array<String>) {

    var language = arrayOf("Ruby", "Koltin", "Python", "Java")

    for (item in language.indices) {

        // printing array elements having even index only
        if (item%2 == 0)
            println(language[item]) } }

When you run the program, the output will be:

Ruby
Python

If you want to learn more about arrays, visit Kotlin arrays.


Iterating Through a String

fun main(args: Array<String>) {

    var text= "Kotlin"

    for (letter in text) {
        println(letter)
    }
}

When you run the program, the output will be:

K
o
t
l
i
n

Similar like arrays, you can iterate through a String with an index. For example,

fun main(args: Array<String>) {

    var text= "Kotlin"

    for (item in text.indices) {
        println(text[item])
    }
}

When you run the program, the output will be:

K
o
t
l
i
n

You will learn to iterate over a map using for loop in Kotin map article.

 

Python Example for Beginners

Two Machine Learning Fields

There are two sides to machine learning:

  • Practical Machine Learning:This is about querying databases, cleaning data, writing scripts to transform data and gluing algorithm and libraries together and writing custom code to squeeze reliable answers from data to satisfy difficult and ill defined questions. It’s the mess of reality.
  • Theoretical Machine Learning: This is about math and abstraction and idealized scenarios and limits and beauty and informing what is possible. It is a whole lot neater and cleaner and removed from the mess of reality.

Data Science Resources: Data Science Recipes and Applied Machine Learning Recipes

Introduction to Applied Machine Learning & Data Science for Beginners, Business Analysts, Students, Researchers and Freelancers with Python & R Codes @ Western Australian Center for Applied Machine Learning & Data Science (WACAMLDS) !!!

Latest end-to-end Learn by Coding Recipes in Project-Based Learning:

Applied Statistics with R for Beginners and Business Professionals

Data Science and Machine Learning Projects in Python: Tabular Data Analytics

Data Science and Machine Learning Projects in R: Tabular Data Analytics

Python Machine Learning & Data Science Recipes: Learn by Coding

R Machine Learning & Data Science Recipes: Learn by Coding

Comparing Different Machine Learning Algorithms in Python for Classification (FREE)

Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.