

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. Println("This developer with number $i rocks: $")įor ((index, value) in names.Now let us run the code, and see the output on the console hi. The in keyword can also be used to check if a value is in a given range!Īnd if you need an index inside your loop, Kotlin allows you to do this in several ways: This syntax can be found in many recent languages, like Swift, for example… The for loop is now used in the form of in. ("This developer rocks: " + name) Įn Kotlin, celle-ci est d’ailleurs devenue beaucoup plus lisible : val names = listOf("Jake Wharton", "Joe Birch", "Robert Martin") Anyone? Well, that’s to be expected! We have all used this type of loop at least once: List names = Arrays.asList("Jake Wharton", "Joe Birch", "Robert Martin") For Once…Īnyone who has never used the for loop in Java, please stand up. Since Kotlin doesn’t bring any special improvement to this way of looping, we’ll skip it. In Kotlin, we have exactly the same thing: // While You probably know the while loop in Java, used to go through a list as long as the condition remains unmet. So this chapter shouldn’t confuse you too much! As Far As Possible If there’s something that hasn’t really changed in Kotlin, it’s the way we loop.
