HTML tutorial
CSS3 tutorial
Bootstrap tutorial
JavaScript tutorial
JQuery tutorial
AngularJS tutorial
React tutorial
NodeJS tutorial
PHP tutorial
Python tutorial
Python3 tutorial
Django tutorial
Linux tutorial
Docker tutorial
Ruby tutorial
Java tutorial
C tutorial
C ++ tutorial
Perl tutorial
JSP tutorial
Lua tutorial
Scala tutorial
Go tutorial
ASP.NET tutorial
C # tutorial
Often when you work with arrays, you need to loop through all of the elements
Often when you work with arrays, you need to loop through all of the elements.
To loop through array elements, use the for
loop together with the in
operator:
Output all elements in the cars array:
val cars = arrayOf("Volvo", "BMW", "Ford", "Mazda")
for (x in cars) {
println(x)
}
You can loop through all kinds of arrays. In the example above, we used an array of strings.
In the example below, we loop through an array of integers:
val nums = arrayOf(1, 5, 10, 15, 20)
for (x in nums) {
println(x)
}
Unlike Java and other programming languages, there is no traditional for
loop
in Kotlin.
In Kotlin, the for
loop is used to loop through arrays, ranges, and other
things that contains a
countable number of values.
You will learn more about ranges in the next chapter - which will create a range of values.