In this article you are going to learn how to perform some operations recursively, how to use for loops, while loops and do while loops.

For loops

For loop is used to iterate through the element of an array or collection. Let’s go through an example to understand how for loop works.

fun printElement(numbers: Array<Int>) {
    for (i in numbers) {
        println(i)
    }
}

fun main(args: Array<String>) {
    printElement(arrayOf(1, 2, 3, 4, 6, 7, 10, 11)
} // Output: 1, 2, 3, 4, 6, 7, 10, 11

In line 2, the variable i is initialized with the first element of an array number i.e i = 1. It is printed in line 3. The control goes to line 2 again and the next element of an array is printed. These steps will continue until we reach the end of our array.

We can also rewrite the given code as

fun printElement(numbers: Array<Int>) {
    for (i in numbers.indices) {
        println(numbers[i])
    }
}

fun main(args: Array<String>) {
    printElement(arrayOf(1, 2, 3, 4, 6, 7, 10, 11)
} // Output: 1, 2, 3, 4, 6, 7, 10, 11

The number.indices return the indexes of an element in the given array. So, in line 2, i is initialized to 0 because the array starts from index 0. In line 3, the element at index 0 is printed. In the next iteration, i is incremented to 1 and so on until the index of the last element of an array.

Let’s see another example of for loop, this time to print element alternatively.

fun printElement(numbers: Array<Int>) {
    for (i in numbers.indices step 2) {
        println(numbers[i])
    }
}

fun main(args: Array<String>) {
    printElement(arrayOf(1, 2, 3, 4, 6, 7, 10, 11)
} // Output: 1, 3, 6, 10

The step allows us to increment the variable i by some number of times i.e 2 in the given example. So, in first iteration i = 0, in next iteration i = 2, then i = 4 and so on until the last index.

If you want to print element in even index within the first four elements of an array then,

fun printElement(numbers: Array<Int>) {
    for (i in 0 until 5 step 2) {
        println(numbers[i])
    }
}

fun main(args: Array<String>) {
    printElement(arrayOf(1, 2, 3, 4, 6, 7, 10, 11)
} // Output: 1, 3, 6

Here, until is used to define the upper limit for the variable i. That means, i can be numbers from 0 to 5 and i is increased by 2 in each iteration.

Opposite to until, downTo is used to define the lower limit and represents decrement. If you want to print the element of an array in reverse order then we can achieve it using downTo as shown in the example.

fun printElement(numbers: Array<Int>) {
    for(i in numbers.size - 1 downTo 0 ) {
         println(numbers[i])
    }
}

fun main(args: Array<String>) {
    printElement(arrayOf(1, 2, 3, 4, 6, 7, 10, 11))
} // output: 11, 10, 7, 6, 4, 3, 2, 1

If you want to print alternate element in reverse order, then use step in the same way as we have seen earlier.

fun printElement(numbers: Array<Int>) {
    for (i in numbers.size - 1 downTo 0 step 2) {
         println(numbers[i])
    }
}

fun main(args: Array<String>) {
    printElement(arrayOf(1, 2, 3, 4, 6, 7, 10, 11))
} // output: 11, 7, 4, 2

While loops

While loops are an alternative to for loops. We can only have a simple condition in while loop where we cannot use the ranges or specify steps like in the for loop.

fun printElement(numbers: Array<Int>) {
    var index = 0
    while(index < numbers.size) {
        println(numbers[index])
        index++
    }
}

fun main(args: Array<String>) {
    printElement(arrayOf(1, 2, 3, 4, 6, 7, 10, 11))
} // Output: 1, 2, 3, 4, 6, 7, 10, 11

Do while loops

Do while loops are very similar to the while loops except that the condition is checked at the end.

fun printElement(numbers: Array<Int>) {
    var index = 0
     do {
        println(numbers[index])
        index++
    } while(index == 0)
}

fun main(args: Array<String>) {
    printElement(arrayOf(1, 2, 3, 4, 6, 7, 10, 11))
} // Output: 1

In the next article, we will learn about how to interrupt the current execution of the loops.

Category
Tags

No responses yet

Leave a Reply

Your email address will not be published. Required fields are marked *