
The test expression inside the parenthesis is a Boolean expression. If you are familiar with while and do.while loops in Java, you are already familiar with these loops in Kotlin as well. You will learn about two loops while and do.while in this article with the help of examples. How about you need to print a sentence one million times? You need to use loops. Well, you can do it by using print statement 50 times (without using loops). Imagine you need to print a sentence 50 times on your screen. Loops are what makes computers interesting machines.

For doing that I simply write if else condition.Loop is used in programming to repeat a specific block of code until certain condition is met (test expression is false). Now suppose I ask to write a program using FOR loop print out all the even numbers starting from 1 to 20. Let’s take another example for better understanding. Overview of FOR loopįinally, the the complete overview looks like below figure. Which will never happen, Because i=4 simply make the condition as false. Now at the end of loop 3 when the value of i becomes 4, This will try to initiate 4 loop. Because this 3 is actually present inside the 1 to 3 range right. Now, i=3 again come the initial value of loop 3, or you can say starting point of 3 loop. 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) 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). 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. What is the exact flow of FOR loop.įor understanding the exact flow, let take an example, see below code 1 Iteration After the loop 3, loops actually terminated. That is loop 1, loop 2 and loop3 has been executed. So here we simply show that println() method has been executed three times.


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