Kotlin Tutorial Beginner Guide for Kotlin Programming Language Tutorial

Loops in Kotlin

In our previous article, we learned about Ranges. But we don’t know how to display all the values inside a range. For this, we are going to use loops. Loops are mainly used for executing a set operation until a certain condition is fulfilled. In Kotlin we have “while”, “do while” and “for” loop. 

Kotlin While Loop Syntax

Below is the syntax for while loop

while(condition){

//while loop body

}

The condition is the expression which returns true or false. while loop executes while body until condition returns true.

While Loop Example

Below are some example for While Loop in Kotlin Programming Language

Example 1: It is going to run indefinitely. Why because “while(true) “ is always true.

Example 2: In this example, we are going print value of "i" until it’s value is less than 6.

Example 3: In this example, we are showing the scenario where we can skip curly braces. Again this loop is going to run indefinitely.

Example 4: In this, we are printing the value of "i" if it is available in range oneToSix. 

/*
    Don't uncomment below loop
    while (true)
    {
        println("Please stop me")
    }
    */
    var i=1
    while(i<6)
    {
        println("Value of i : $i")
        i++
    }

    //We can skip curly braces if loop has only one statement
    //while (true) println("Please stop me")

    var oneToSix = 1..6
    while(i in oneToSix)
    {
        println("Value of i : $i")
        i++
    }

 

do while Loop in Kotlin Programming Language.

do while loop is same as while loop, with one key difference. In while loop, we check condition fire and then execute the code inside loop’s body. In case of "do while" loop’s body executes at least once.

Kotlin do while Loop Syntax

do {

// loop body

} while(condition)

do while loop examples

Following code shows some example of “do while” loop in Kotlin Programming Language.

var j=0
    var oneToTen=1..10
    do{
        println("Value of j: $j")
        j++
    }
    while(j in oneToTen)

    do{
        println("Value of j: $j")
    }
    while(j<=5)

 

Finally, It’s time to check For loop. In Kotlin Programming Langauge for loop is different as compared with other Programming Languages such as Java, PHP, or C#.

In Kotlin for loop is used as an iterator. It can iterate through anything that provides an iterator. You can say Kotlin for loop is equivalent to the “foreach” loop in other programming languages. Following is the syntax of for loop.

Kotlin for loop Syntax

for (item in collection) {

// body

}

Following are some examples How to use for loop in Kotlin Programming Langauge.

The first set of example shows How to Iterate through a Range using for loop in Kotlin

The second set of example shows How to iterate through a string

Finally, How to iterate through an array.

//Example: Iterate Through a Range
    var zeroToNine = 0..9
    for(number in zeroToNine)
    {
        println("Number : $number")
    }

    for(number in 3..1)
    {
        println("Number : $number")
    }

    for(alphabets in 'a'..'z') println("English Alphabets : $alphabets")

    for (number in 6 downTo 2) println("English Alphabets : $number")

    for (number in 1..10 step 2) println("Odd Numbers : $number")

    var k=10
    for (number in 30 downTo 3 step 3) {
        println("3 * $k : $number")
        k--
    }


    //Example String
    var authorName = "Mayank Sanghvi"
    for(letters in authorName) println("Author Name Letter : $letters")

    for(lettersIndices in authorName.indices) println("Author Name Letter : "+authorName[lettersIndices] + " Position $lettersIndices")


    //Example Arrays
    var vLemonnTutorials = arrayOf("Android", "Koltin", "Java" ,"PHP", "C#", "Unity 3D")
    for(tutorials in vLemonnTutorials) println("Mayank Teach : $tutorials")

    for(tutorialIndices in vLemonnTutorials.indices) println("Mayank Teach : "+vLemonnTutorials[tutorialIndices])

 

0 Comments
Leave A Comment

Please login to post your valuable comments.

share