目录
一、认识
二、for循环
1、for-in
2、forEach
3、forEachIndexed
三、while和do-while
1、while
2、do-while
四、区间迭代器
1、in
2、until
3、downTo
4、step
五、break和continue
1、break
2、continue
1、for循环:语法for(item in array)
2、while和do-while:while当执行条件为true时,循环执行
do-while先执行一次循环,后续条件为true时,循环执行
3、迭代区间:in,until,downTo-step
4、break 和 continue: break 跳出循环
continue 跳出本次循环,执行下一次
val array = arrayListOf("kotlin","java","c#","android")
for(i in array){
println(i)
}
val array = arrayListOf("kotlin","java","c#","android")
array.forEach{
println(it)
}
val array = arrayListOf("kotlin","java","c#","android")
array.forEachIndexed{
index, s ->
println("$index , $s")
}
判断循环条件,当条件满足,执行循环体,不满足,退出循环
val array = arrayListOf("kotlin","java","c#","android")
var index = 0
while (index
先执行一次语句,再去判断循环条件
val array = arrayListOf("kotlin","java","c#","android")
var index1 = 0
do{
println("the $index1 elements is ${array[index1++]}")
}while (index1
for(i in 1..10){
print(i)
}
//12345678910
左闭右开
for(i in 1 until 10){
print(i)
}
//123456789
倒序
for(i in 10 downTo 1){
print(i)
}
//10987654321
步长
for(i in 1..10 step 2 ){
print(i)
}
//13579
退出循环体
for(i in 1..10){
if(i>5)
break
print(i)
}
//12345
退出本次循环,执行下一次循环
for(i in 1..10){
if(i==5)
continue
print(i)
}
//1234678910