Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




KOTLIN - FOR LOOP


The 'for loop' in Kotlin is like the 'while loop'. But the 'for loop' is way more powerful than the 'while loop'.


The 'for loop' has inbuilt features, by which you can reduce lines of code.


So far we have seen, how 'while loop' was used to print "Hello World" 5 times.


Example :



fun main(){
    var x = 1
    while (x <= 5) {
        println("Hello World")
        x = x + 1
    }
}

The same program could be reduced to less lines of code using the 'for loop'.


Let us see below.


Example of 'for' loop :


Let us use 'for' loop to print "Hello World" five times on the screen.


Example :



fun main(){
    for(i in 1..5) {   
        println("Hello World")
    } 
}


Output :



 Hello World
 Hello World
 Hello World
 Hello World
 Hello World

Now, if we break the contents of 'for' loop, we get :



  1. Initialisation part : It is run before the loop starts.

    for(i in 1..5)


    'i' is getting initialized with value '1'. So, the value '1' before the two dots (i.e. 1..5) is getting initialized to 'i'.


  2. Conditional part : It states the condition for running the loop.

    for(i in 1..5)


    It says, continue to be in the loop until the value of 'i' is not more than '5'. So, the value '5' after the two dots (i.e. 1..5) is considered as the conditional statement.


  3. Increment part : It is run each time after the loop.

    You do not have to worry, as it happens internally in Kotlin 'for loop'. i.e. Every time the value gets incremented by '1'.

All together forms the 'for' loop.



for(i in 1..5) {   
    println("Hello World")
}

So in the 1st iteration,


1st Iteration


The control gets into the 'for loop' and checks, if the value of 'i' is less than or equal to '5'.


java_Collections

In this case the value of 'i' is '1'. Which is less than '5'.


So, the control enters the 'for loop'.



for(i in 1..5) {   
    println("Hello World")
}

and finds the print statement,



println("Hello World")

So, it prints 'Hello World'.


Output :



 Hello World

Next, Kotlin finds, it has come to the end of the loop.


java_Collections

So, it increments the value of 'i' by 1 (And Kotlin does the increment internally).


And the value of 'i' becomes '2'.


java_Collections

And the second Iteration starts.


2nd Iteration


Same way, the control comes to the 'for loop' and checks, if the value of 'i' is less than or equal to '5'(i.e. Internally Kotlin checks if 'i <= 5').


java_Collections

In this case the value of 'i' is '2'. Which is less than '5'.


So, the control enters the 'for loop'.


java_Collections

And finds the print statement,



println("Hello World")

So, it prints 'Hello World' for the second time.


Output :



 Hello World
 Hello World

Similarly, Kotlin finds, it has come to the end of the loop.


java_Collections

And increments the value of 'i' by '1'.


java_Collections

Similarly, the third, fourth and fifth Iteration continues incrementing the value of 'i',printing it.


3rd Iteration


Output :



 Hello World
 Hello World
 Hello World

java_Collections

4th Iteration


Output :



 Hello World
 Hello World
 Hello World
 Hello World

java_Collections

5th Iteration


Output :



 Hello World
 Hello World
 Hello World
 Hello World
 Hello World

java_Collections

Once again, the control comes to the 'for loop' and checks, if the value of 'x' is less than or equal to '5'.


java_Collections

In this case, the value of 'x' is '6'. Which more than '5'.


And the control comes out of the 'for loop'.


Now, if you see the output, 'Hello World' is exactly printed 5 times.


So, we have seen, how to print 'Hello World' 5 times.


Now, let us see, how can we print the numbers from 1 to 5.


Printing the numbers 1 to 5 using 'for' loop


Example :



fun main(){
    for(i in 1..5) {   
        println(i)
    } 
}	


Output :



 1
 2
 3
 4
 5

The above code is self explanatory.


However, we have provided the explanation below.



CLICK HERE TO SEE THE EXPLANATION.


So in the 1st iteration,


1st Iteration


The control gets into the 'for loop' and checks, if the value of 'i' is less than or equal to '5'.


java_Collections

In this case the value of 'i' is '1'. Which is less than '5'.


So, the control enters the 'for loop'.



for(i in 1..5) {   
    println(i)
}

and finds the print statement,



println(i)

So, it prints the value of 'i', which is currently '1'.


Output :



 1

Next, Kotlin finds, it has come to the end of the loop.


java_Collections

So, it increments the value of 'i' by 1 (And Kotlin does the increment internally).


And the value of 'i' becomes '2'.


java_Collections

And the second Iteration starts.


2nd Iteration


Same way, the control comes to the 'for loop' and checks, if the value of 'i' is less than or equal to '5'(i.e. Internally Kotlin checks if 'i <= 5').


java_Collections

In this case the value of 'i' is '2'. Which is less than '5'.


So, the control enters the 'for loop'.


java_Collections

And finds the print statement,



println(i)

So, it prints the value of 'i', which is '2' currently.


Output :



 1
 2

Similarly, Kotlin finds, it has come to the end of the loop.


java_Collections

And increments the value of 'i' by '1'.


java_Collections

Similarly, the third, fourth and fifth Iteration continues incrementing the value of 'i',printing it.


3rd Iteration


Output :



 1
 2
 3

java_Collections

4th Iteration


Output :



 1
 2
 3
 4

java_Collections

5th Iteration


Output :



 1
 2
 3
 4
 5

java_Collections

Once again, the control comes to the 'for loop' and checks, if the value of 'x' is less than or equal to '5'.


java_Collections

In this case, the value of 'x' is '6'. Which more than '5'.


And the control comes out of the 'for loop'.


So, we have seen, how to print the numbers from 1 to 5.


Now, what if, we want to print the numbers from 5 to 1.


Will the below 'for' loop work?


for(i in 5..1) {
    println(i)
}	

Well! The answer is NO.


Let's see, how can we print the numbers from 5 to 1.


Printing the numbers 5 to 1 using 'for' loop and downTo


Example :



fun main(){
    for(i in 5 downTo 1 ) {   
        println(i)
    } 
}	


Output :



 5
 4
 3
 2
 1

And in the above example, we have used 'downTo' with the 'for' loop.


for(i in 5 downTo 1 )

It says, start from the value '5' and go all the way till '1'.


So, the Initialisation part/Initial value of 'x' is,


java_Collections

And the Conditional part is,


java_Collections

So, the above 'for' assigns the initial value '5' to 'i' and continues to be in the loop until the value of i is greater than or equal to '1'.


The moment, value of 'i' is '0'(i.e. Less than 1) the control comes out of the loop.


Now, what if you want to skip a value and want to print the odd numbers between 1 to 10.


Printing the odd numbers between 1 to 10 using 'step'


Example :



fun main(){
    for(i in 1..10 step 2) {   
        println(i)
    } 
}	


Output :



 1
 3
 5
 7
 9

So, if you look at the output, you can see that the odd numbers between 1 to 10 is printed on the screen.


And the magic happens when we use 'step' in the 'for' loop.


for(i in 1..10 step 2)

Internally what happens is, the 'for' loop is asked to print all the values from 1 to 10.


java_Collections

Then comes the 'step' keyword. We have given '2' after the 'step' keyword. Which says skip one number and print the next.


java_Collections

And we get all the odd numbers printed.


Example of 'for' 'each' loop:


We will be learning about it in the Arrays section of Ruby.