Thursday, 9 April 2020

Loops In Kotlin

Loops

The loops in Kotlin are similar to other programming languages. Loops basically provide the flexibility to iterate over any kind of data structure.

For Loop

To iterate over any data structure, It should be iterable. for loop always implicitly declares a new read-only variable in its scope and it's not accessible outside the loop scope. Here is a code snippet that will clear the things more:

val names = listOf("Ram", "Syam", "Geta")
for (name in names) {
        println(name)  // Print the names present in the 
}

 println(name) // name is not available outside the loop scope

Output:
Ram
Syam
Geta


  • We can also get the index along with the value while iterating by for loop. Here is the code snippet:

 // first variable would be always index
for ((index, name) in names.withIndex()) { 
        println("index $index, name $name ")
}

Output:
index 0, name Ram 
index 1, name Syam 
index 2, name Geta 


  • Alternatively, We can also iterate an array or list using indices. Here is the code snippet that is self-explanatory:

for (index in names.indices) {
        println("name ${names[index]} ")
}

Output: 
name Ram 
name Syam 
name Geta 


  • There is another flexibility that we can use the range operator (..) to iterate the loop. Here is the code snippet that is self-explanatory:

for (number in 0..10) { // It would print from 0 to 10 along with 0 and 10
        print("$number, ")
}  

Output: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,



  • There is another flexibility that we can exclude the last value using until while iterating the loop. Here is the code snippet:

for (number in 0 until 10) { // It would print till 9
        print("$number, ")
}

Output: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 


  • There is another flexibility that we can control and specify the increment steps like increment by 2, so at every iteration, the increment would be 2. Here is the code snippet that is self-explanatory:

for (number in 0 until 10 step 2) { // step must be positive always
        print("$number, ")

Output: 0, 2, 4, 6, 8, 

Note: Step must be positive always otherwise you would get Runtime Exception: java.lang.IllegalArgumentException: Step must be positive, was: -2


  • There is another flexibility that we can iterate in the reverse order (count downwards), To achieve it we can use downTo. Here is the code snippet that is self-explanatory:

for (number in 10 downTo 0 step 2) {
        print("$number, ")
}

Output: 10, 8, 6, 4, 2, 0, 


  • We can also iterate the map using for loop. Here are various ways to iterate over the map. Here is the code snippet that is self-explanatory:

 val map = mutableMapOf(1 to "x", 2 to "y", 3 to "z")

// Iterating the map with every entry object
    for (entry in map) {
        print("${entry.key}: ${entry.value}, ")
    }

Output: 1: x, 2: y, 3: z, 

// Iterating the map with key and value objects
    for ((key, value) in map) {
        print("$key: $value, ")
    }

Output:  1: x, 2: y, 3: z, 

// Iterating the map with key objects
    for (key in map.keys) {
        print("$key, ")
    }

Object: 1, 2, 3, 

//Iterating the map with value objects
    for (value in map.values) {
        print("$value, ")
    }

Output: x, y, z, 

while and do..while Loop

In Kotlin programming language also while and do..while loop works like other programming languages. Here is the code snippet that is self-explanatory:

var num = 0

// while Loop
    while (num < 10) {
        print("$num, ")
        num++
    }


// do..while loop
num = 0
    do {
        print("$num, ")
        num++
    } while (num < 10)

Output: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 


I think we have gained enough knowledge regarding loops in Kotlin.

Happy Learning!!




1 comment:

Visibility Modifier In Kotlin

Visibility Modifier The visibility modifier basically defines a scope from where it can be visible and accessible. There are four visi...