syntax:
for(item in Collection){
// statement
}
for in here functions like for each in java. item inside the parenthesis is an element with the same data type for each collection element. collection can be a list, an array, a map or anything that groups the same related data. It can also be a range(..) or downTo which is the opposite of range.
==================
fun main(){
for(i in 0..100 step 10){
print("$i ")
}
}
==================
Result:
0 10 20 30 40 50 60 70 80 90 100
Note: step functions as an infix operator which followed by a number which specify by how much per step.
=================
fun main(){
val yourArray = Array(3){it * 10}
for((x, y) in yourArray.withIndex()){
println("$x: $y")
}
}
=================
Result:
0: 0
1: 10
2: 20
Note: "it" represents each element in an array. it * 10 means to multiply each element by 10. withIndex() method is used since this was program to give two outputs which is the index and the value for each element in the array. You can used the indices, iterator(), next() or hasNext() which we often see in a while loop.
STAI LEGGENDO
Kotlin Programming
CasualeI have been trying to learn things online. I will put my notes in here. I put things in my own words and i do not copy their programs. I try to do things the right way so that i may learn. Below is the site where i study. Go check this out if you wa...
