Sharing our technical trials in terms of Ubuntu software installs, Blogger hacks, Android and Kotlin learning.
How to use control flow statements (Loops) in Kotlin
How to use control flow statements (Loops) in Kotlin

How to use control flow statements (Loops) in Kotlin

Control flow statements or Loops in Kotlin are used to carry out repetitive task’s by repeating certain set of code.

There are three kinds of control flow statements or loops in kotlin

1. for

2. while

3. do..while

We will be seeing the syntax and examples for each one of these.

1. For Loop

Syntax
for (item in array) {
        // the code goes here
  }
  • starts with a keyword “for”
  • followed by brackets ( ) in which the condition for looping is mentioned, which is usually range
  • And the code that needs to be repeated as long as the condition satisfies is mentioned under { }
Example 1
 fun main (args:Array<String>)
{

 var fruits = arrayOf("apple","banana","cherry","pear","strawberry")

      for (item in fruits) {
            println(item)
      }
}
Output

apple
banana
cherry
pear
strawberry

Explanation

fruits – array of type string with names of fruits

  • for (item in fruits) – for each item in an array “fruits”
  • println(item) – print the item in an array
Example 2

Array.indices – can be used to get the indexes in an array

 fun main (args:Array<String>)
{

 var fruits = arrayOf("apple","banana","cherry","pear","strawberry")

 for (item in fruits.indices) {
            print(item)
      } 
}
Output

0
1
2
3
4

Explanation

fruits – array of type string with names of fruits

  • for (item in fruits.indices) – fruits.indices refers to the indices in the array
  • println(item) – would print the indices

Example 3

Alternatively we can use a library function withIndex to access index and the value together

 fun main (args:Array<String>)
{

 var fruits = arrayOf("apple","banana","cherry","pear","strawberry")

for ((index, value) in fruits.withIndex()) {
            println("$index is $value")
      }
}
Output

0 is apple
1 is banana
2 is cherry
3 is pear
4 is strawberry

Explanation

fruits – array of type string with names of fruits

  • for ((index, value) in fruits.withIndex()) -withIndex() is a library function, to access index and value
  • println(“$index is $value”) – we are printing index and value
Example 4

We can also explicitly mention the range

 fun main (args:Array<String>)
{
       var fruits = arrayOf("apple","banana","cherry","pear","strawberry")
        for (i in 1..3) {
         println(fruits[i])
      }  
}
Output

banana
cherry
pear

Explanation

fruits – array of type string with names of fruits

  • for (i in 1..3) – i indicates the index, so the condition mentions for index in the range 1 to 3
  • println(fruits[i]) – we are printing the item from the fruits array, in the index range 1 to 3
Example 5

With range you can also use something called as step – to indicates the differences between the indices

 fun main (args:Array<String>)
{
    var fruits = arrayOf("apple","banana","cherry","pear","strawberry")
    for (i in 1..3 step 2) {
            println(fruits[i])
      }  
}
Explanation

indicates indices in the range 1 to 3 with differences between the indices as 2

Example 6

Usage of downTo function

 fun main (args:Array<String>)
{
    var fruits = arrayOf("apple","banana","cherry","pear","strawberry")
     for (i in 4 downTo 0) {
            println(fruits[i])
      }
}
Output

strawberry
pear
cherry
banana
apple

Explanation

fruits – array of type string with names of fruits

for (i in 4 downTo 0)

  • i indicates the index
  • 4 downTo 0 – from index 4 down to index 0
  • println(fruits[i]) – we are printing the item from the fruits array starting from 4th index till 0th index
Example 7

downTo can also be used with step – step indicates the differences between the indices

 fun main (args:Array<String>)
{

 var fruits = arrayOf("apple","banana","cherry","pear","strawberry")

  for (i in 4 downTo 0 step 2) {
            println(fruits[i])
      }
}
Explanation

indicates indices in the range 4 downTo 0 with differences between the indices as 2

Example 8

Usage of until function

 fun main (args:Array<String>)
{

     var fruits = arrayOf("apple","banana","cherry","pear","strawberry")
     for (i in 0 until 3) {  
            print(fruits[i])
      }
}
Output

apple
banana
cherry

Explanation

fruits – array of type string with names of fruits

for (i in 0 until 3)

  • i indicates the index
  • 0 until 3 – from index 0 to index 3 , excluding index 3
  • println(fruits[i]) – we are printing the item from the fruits array in the range as per the condition

We will be seeing the other two looping statements in out next post Control Flow (Loops) cont’d .