# FOR loop in Kotlin –

In this blog we will discover the FOR loop in the Adnroid boiler, we will see the exact flow of the loop. Let’s look in detail at how this should work using the FOR loop as an example. Then let’s get started.

#### FOR LOOP SYNTAX

For a FOR loop, the syntax is space, open and closed parentheses. Internally I just used the variable i followed by the operator and defined the range.

for (fork initializer) {
}

##### Let’s look at a simple example.

fun main(args: Array) {
// For loop
for (i in 1…20) {
println(i)
}
}

#### For example, loop

Let’s take a very simple example of the FOR loop, open the IDE and paste the code below.

fun main(args: Array) {
for (i in 1…3) {
println(hi…)
}
}

Now let’s run the code and see the output on the console….

Hello, hello, hello… Hi, um…
Process completed with output code 0

So here we just show that the println() method was executed three times. In other words, cycles 1, 2 and 3 are complete. After loop 3 the loops are actually finished.

##### What is the exact sequence of the FOR loop.

To understand the exact procedure, we take an example, see the following code

#### 1 repetition

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

#### 2 Repetition

Now comes the value of i=2 to vary the starting point of the 2 iterations. If during the second iteration we need to check condition 2 again, it is in the range of 1 to 3, and the condition becomes true again, and we print hi again, at the end of the iteration, we simply increase the value of I and I becomes 3 (i=3).

Now, with i=3, the initial value of cycle 3 returns, or can we say the initial point of cycle 3. And the state is gaining momentum. Because this 3 is actually present in the range of 1 to 3 on the right.

Then let’s say hello again. Finally, the value of i becomes 4 (i=4). Now, at the end of cycle 3, when the value of i becomes 4, we try to start cycle 4. Which will never happen because i=4 makes the condition wrong. A four is out of range. That’s how the loop ends up being correct.

#### Loop overview for

The overview will eventually look like the following figure.

#### For example, loop

Let’s take another example to understand it better. Now suppose I ask you to write a program that uses the FOR loop to print all even numbers from 1 to 20. For this I just write if there are other conditions. As indicated below

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

Now let’s run the code. So two, four, six, eight… 20. With this we print all the event numbers in the sample loop.

2
4

..
18
20
Process completed with output code 0

#### Conclusion

In this article we learned how the FOR cycle works in the case of Kotlin. Thanks for reading, have a great day.

#### Related Tags:

for loop in kotlin arraylistfor loop in kotlin stackoverflowkotlin for loop rangekotlin foreach loopnested for loop in kotlinindex wise for loop in kotlinarray in kotlinkotlin for loop with condition 