val names = listOf("Root", "Java", "Lua","Flutter", "R&B", "Crips")
for (name in names) {
println(name)
}
for(name in names)
names 代表上方所宣告的List集合
name 是在迴圈中所使用的變數 盡量不要與外部宣告變數重疊
for(x in 0..10)
Log.d("xxx","$x")
輸出 0-10
for (x in 0 until 10)
Log.d("xxo","$x")
輸出 0-9
for (x in 0 until 10 step 2)
Log.d("xxoo","$x")
输出 0, 2, 4, 6, 8
for (x in 10 downTo 0 step 2)
println(x)
输出 10, 8, 6, 4, 2, 0
val numbers = (0..9).toList()
Log.d("xxooO","$numbers")
輸出 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
val names = listOf("Root", "Java", "Lua","Flutter", "R&B", "Crips")
for ((index, value) in names.withIndex()) {
println("$index: $value")
}
輸出 0:Root 1:Java 2:Lua 3:Flutter 4:R&B 5:Crips
var x = 0
while (x < 10) {
println(x)
x++
}
x++ 等同于 x += 1