Kotlin⚡️: Loops | Erselan Khan

1_JDbMlhYgDsx5kbuUMO98pA.png

Today we will show you how we can use different types of Loops in the Kotlin programming language.

But before moving forward, I would like to ask you to please follow my medium account to get the latest updates about Android and other tech-related topics and also check out my previous part of this series here Link.

What is Loop?

In computer programming, a loop is a sequence of instructions that is repeated until a certain condition is reached. Programmers use loops to cycle through values, add sums of numbers, repeat functions, and many other things.

Kotlin: 🖖

In the case of the Kotlin programming language, we can use a loop in four different ways:

  1. For Loop (using in keyword)
  2. For Loop (using until keyword)
  3. Foreach Loop
  4. While Loop

Let’s write some code examples in the Kotlin programming language:

1. For Loop (using in keyword):

                /*
1. for loop using "in"
*/
for(i in 0..5) {
    println("for loop using in")
}
            

In the above example, we are using range expression. The above for loop prints the line by 6 times as we define the range from 0 to 5.

2. For Loop (using until keyword):

                /*
2. for loop using "until"
*/
for (i in 0 until 5) {
    println("for loop using until")
}
            

In the above example, we are using the range expression keyword i.e until. The above for loop prints the line by 5 times, that's means it excludes 5 from the loop as it’s the end element.

3. Foreach Loop:

                /*
3. foreach on List 1st example
*/
val names: List<String> = listOf("Erselan", "Khan")
names.forEach {
    println(it)
}
/*
4. foreach on List 2nd example
*/
for (name in names) { // names is the list that we defined above 
    println(name)
}
            

we can define a foreach loop like above in Kotlin. In our first example, we are using the forEach to iterate each of the elements inside the list. In our second example, we are using the traditional way of iterating elements in Kotlin.

4. While Loop:

                /*
5. while loop
*/
var i = 5
while (i > 0) {
    println("while loop")
    i--
}
            

In our above example, we are using the while loop. While loop checks the condition each time, and if the condition is not satisfied then it breaks the loop. We are printing the line 5 times as the condition will not be satisfied on (0>0) and eventually, it breaks the while loop.

Here is the full example:

                fun main() {

    /*
    1. for loop using "in"
    */
    for(i in 0..5) {
        println("for loop using in")
    }

    /*
    2. for loop using "until"
    */
    for (i in 0 until 5) {
        println("for loop using until")
    }

    /*
    3. foreach on List
    */
    val names: List<String> = listOf("Erselan", "Khan")
    names.forEach {
        println(it)
    }

    /*
    4. foreach on List 2nd example
    */
    for (name in names) {
        println(name)
    }

    /*
    5. while loop
    */
    var i = 5
    while (i > 0) {
        println("while loop")
        i--
    }
}
            

That’s it for now. I will cover more topics on Android, Java, Kotlin, and Springboot in my upcoming articles.

In case you missed: 🤐

  1. https://erselankhan.medium.com/kotlin-%EF%B8%8F-null-safety-erselan-khan-857277661bd
  2. https://towardsdev.com/android-kotlin-vs-java-series-part-5-erselan-khan-90b5a1dfcd73
  3. https://medium.com/bazaar-tech/dynamically-update-refresh-reload-viewpager2-fragments-588fcbd6f859

Show your love ❤️ by sharing this article with your fellow developers 😅 and also following my Medium account ✈️

GIF Source: https://blog.hubspot.com/marketing/gif-websites

(Again, the source for this demo is on https://github.com/arsalankhan994/kotlin-examples. Follow me for more content about Android, Kotlin, and other technologies. If you have any questions, go ahead and ask me here or email me at arsalankhan994@gmail.com and I’ll do my best to respond.)


Only registered users can post comments. Please, login or signup.

Start blogging about your favorite technologies and get more readers

Join other developers and claim your FAUN account now!

Avatar

Erselan Khan

Senior Software Engineer, Bazaar Technologies

@erselankhan
Coding, Coding and Coding :')
Stats
22

Influence

978

Total Hits

1

Posts

Discussed tools