1for (i in 1..5) print(i)
2-> 12345
3
4for (i in 5 downTo 1) print(i)
5-> 54321
6
7for (i in 3..6 step 2) print(i)
8-> 35
9
10for (i in 'd'..'g') print (i)
11-> defg
1val array = arrayOf(1, 3, 9)
2for (item in array) {
3 //loops items
4}
5for (index in 0..array.size - 1) {
6 //loops all indices
7}
8for (index in 0 untill array.size) {
9 //loops all indices
10}
11for (index in array.indices) {
12 //loops all indices (performs just as well as two examples above)
13}
1val school = arrayOf("shark", "salmon", "minnow")
2for (element in school) {
3 print(element + " ")
4}
5-> shark salmon minnow
6
7for ((index, element) in school.withIndex()) {
8 println("Item at $index is $element\n")
9}
10-> Item at 0 is shark
11Item at 1 is salmon
12Item at 2 is minnow
1val names = listOf("Jack", "John", "Tim")
2for(name in names){
3 println(name)
4}
1for (i in 1..5) print(i) // "12345"
2for (i in 5 downTo 1) print(i) // "54321"
3for (i in 0 until 5) { // "01234"
4 println(i)
5}
6for(i in 0 until list.size()){
7 println(i)
8}