Kotlin Fundamental

FOR loop in Kotlin

Pinterest LinkedIn Tumblr

In this blog, we’ll learn FOR loop in kotlin Adnroid, will see the exact flow of for loop. How it will work, Will understand the working of FOR loop in kotlin detail with the help of an example. So let’s started

FOR LOOP SYNTAX

FOR loop the syntax is for followed by space, bracket open and close. Inside this I simply used the variable of i followed by in operator and defined the range.

for loop syntax
for (initializer in ranges) {
   // put your code here
}
Lets take a simple example
fun main(args: Array<String>) {
    // For loop
    for (i in 1..20) {
        println(i)
    }
}

FOR loop example

Let take a very basic example of FOR loop, Open the IDE and paste below code

fun main(args: Array<String>) {
    for (i in 1..3) {
        println("hi..")
    }
}

Now let us run the code, and see the output on the console

hi..
hi..
hi..
Process finished with exit code 0

So here we simply show that println() method has been executed three times. That is loop 1, loop 2 and loop3  has been executed. After the loop 3, loops actually terminated.

What is the exact flow of FOR loop.

For understanding the exact flow, let take an example, see below code

1 Iteration

In the first iteration( loop 1) the value of i is actually 1 (i=0)and next step comes to the condition check so 1 falls inside the 1 to 3 range. So the condition becomes true then we simply ‘print hi’, and finally at end of the loop simply increment the value of i by 1, So now value of i becomes 2 (i=2).

2 Iteration

Now value i=2 comes the vary initial point of 2 iterations. In the case in second iteration again we have to condition check 2 actually falls inside the range 1 to 3 range, and the condition become true again and again we ‘print hi’, at end of iteration simply increment the value of I and i become 3 (i=3)

for-loop-second-loop

Now, i=3 again come the initial value of loop 3, or you can say starting point of 3 loop. And gain condition becomes true. Because this 3 is actually present inside the 1 to 3 range right. 

So again we’ll print hello. And finally value of i becomes 4 (i=4). Now at the end of loop 3 when the value of i becomes 4, This will try to initiate 4 loop. Which will never happen, Because i=4 simply make the condition as false. The 4 does not lie inside the range. So finally the loop terminates right.

Overview of FOR loop

Finally, the the complete overview looks like below figure.

How for loop work in kotlin

FOR loop example

Let’s take another example for better understanding. Now suppose I ask to write a program using FOR loop print out all the even numbers starting from 1 to 20. For doing that I simply write if else condition. Such as below

fun main(args: Array<String>) {
    // For loop
    for (i in 1..20) {
        if (i % 2 == 0)
            println(i)
    }
}

So let us now run  the code. So here we go 2,4,6,8…,20. So we prints all event number using this for loop example.

2
4
..
..
18
20
Process finished with exit code 0

Conclusion

In this post, we have learned How does FOR loop works in the case of Kotlin. Thank for reading, Have a Good Day. 

Write A Comment