版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/likui19921224/article/details/80091876
常见循环:for while
for中包含了 foreach 和for while 中包含了 while 和dowhile 和while do(和java一样)
for代码:
使用方式1:
val s1= listOf(2,3,4);
for (i in s1) println(i) 遍历参数
使用方式2:
val s1= listOf(2,3,4);
for (index in s1.indices) println(index) 遍历下标:indices
使用方式三:foeach
val s1= listOf(2,3,4);
s.forEach(){
println(it);
}
使用方式4 ----省略s1
s.indices.foreach(){
println(it)
}
while 和java一样
–前面说过控制流都 是表达式:因此上面也可以使用返回值的