valitems=listOf("apple","banana","kiwifruit")for(indexinitems.indices){println("item at $index is ${item[index]})
}
while loop
1 2 3 4 5 6
valitems=listOf("apple","banana","kiwifruit")varindex=0while(index<items.size){println("item at $index is $(items[index]))
index++}
when expression
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
fundescribe(obj:Any):String=when(obj){1->"One""Hello"->"Greeting"isLong->"Long
!isString->"Not a string"else->"Unknown"}funmain(){println(describe(1))// Oneprintln(describe("Hello"))// Greetingprintln(describe(1000L))// Longprintln(describe(2))// Not a Stringprintln(describe("other"))// Unknown}
Ranges
1 2 3 4 5
valx=10valy=9if(xin1..y+1){println("fits in range")}
1 2 3 4 5 6 7 8
vallist=listOf("a","b","c")if(-1!in0..list.lastIndex){println("-1 is out of range")}if(list.size!inlist.indices){println("list size is out of valid list indices range, too")}