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.
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.
Let us use 'for' loop to print "Hello World" five times on the screen.
fun main(){ for(i in 1..5) { println("Hello World") } }
Now, if we break the contents of 'for' loop, we get :
All together forms the 'for' loop.
for(i in 1..5) { println("Hello World") }
So in the 1st iteration,
The control gets into the 'for loop' and checks, if the value of 'i' is less than or equal to '5'.
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,
So, it prints 'Hello World'.
Next, Kotlin finds, it has come to the end of the loop.
So, it increments the value of 'i' by 1 (And Kotlin does the increment internally).
And the value of 'i' becomes '2'.
And the second Iteration starts.
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').
In this case the value of 'i' is '2'. Which is less than '5'.
So, the control enters the 'for loop'.
And finds the print statement,
So, it prints 'Hello World' for the second time.
Similarly, Kotlin finds, it has come to the end of the loop.
And increments the value of 'i' by '1'.
Similarly, the third, fourth and fifth Iteration continues incrementing the value of 'i',printing it.
Once again, the control comes to the 'for loop' and checks, if the value of 'x' is less than or equal to '5'.
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.
fun main(){ for(i in 1..5) { println(i) } }
The above code is self explanatory.
However, we have provided the explanation below.
CLICK HERE TO SEE THE EXPLANATION.
So in the 1st iteration,
The control gets into the 'for loop' and checks, if the value of 'i' is less than or equal to '5'.
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,
So, it prints the value of 'i', which is currently '1'.
Next, Kotlin finds, it has come to the end of the loop.
So, it increments the value of 'i' by 1 (And Kotlin does the increment internally).
And the value of 'i' becomes '2'.
And the second Iteration starts.
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').
In this case the value of 'i' is '2'. Which is less than '5'.
So, the control enters the 'for loop'.
And finds the print statement,
So, it prints the value of 'i', which is '2' currently.
Similarly, Kotlin finds, it has come to the end of the loop.
And increments the value of 'i' by '1'.
Similarly, the third, fourth and fifth Iteration continues incrementing the value of 'i',printing it.
Once again, the control comes to the 'for loop' and checks, if the value of 'x' is less than or equal to '5'.
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.
fun main(){ for(i in 5 downTo 1 ) { println(i) } }
And in the above example, we have used 'downTo' with the 'for' loop.
It says, start from the value '5' and go all the way till '1'.
So, the Initialisation part/Initial value of 'x' is,
And the Conditional part is,
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.
fun main(){ for(i in 1..10 step 2) { println(i) } }
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.
Internally what happens is, the 'for' loop is asked to print all the values from 1 to 10.
Then comes the 'step' keyword. We have given '2' after the 'step' keyword. Which says skip one number and print the next.
And we get all the odd numbers printed.
We will be learning about it in the Arrays section of Ruby.