Kotlin 循环控制

迷南。 2022-11-27 07:15 210阅读 0赞

文章目录

  • For 循环
  • while 与 do…while 循环
  • 返回和跳转
  • Break 和 Continue 标签
  • 标签处返回
  • END

For 循环

for 循环可以对任何提供迭代器(iterator)的对象进行遍历,语法如下:

  1. for (item in collection) print(item)

循环体可以是一个代码块:

  1. for (item: Int in ints) {
  2. // ……
  3. }

如上所述,for 可以循环遍历任何提供了迭代器的对象。
通过索引遍历

  1. for (i in array.indices) {
  2. print(array[i])
  3. }

用库函数 withIndex:

  1. for ((index, value) in array.withIndex()) {
  2. println("the element at $index is $value")
  3. }

示例:

  1. fun main(args: Array<String>) {
  2. val items = listOf("apple", "banana", "kiwi")
  3. for (item in items) {
  4. println(item)
  5. }
  6. for (index in items.indices) {
  7. println("item at $index is ${
  8. items[index]}")
  9. }
  10. }

结果:

  1. apple
  2. banana
  3. kiwi
  4. item at 0 is apple
  5. item at 1 is banana
  6. item at 2 is kiwi

while 与 do…while 循环

表达式:

  1. while( 布尔表达式 ) {
  2. //循环内容
  3. }

do…while 循环 对于 while 语句而言,如果不满足条件,则不能进入循环。但有时候我们需要即使不满足条件,也至少执行一次。

do…while 循环和 while 循环相似,不同的是,do…while 循环至少会执行一次。

  1. do {
  2. //代码语句
  3. }while(布尔表达式);

示例:

  1. fun main(args: Array<String>) {
  2. println("----while 使用-----")
  3. var x = 5
  4. while (x > 0) {
  5. println( x--)
  6. }
  7. println("----do...while 使用-----")
  8. var y = 5
  9. do {
  10. println(y--)
  11. } while(y>0)
  12. }

结果:

  1. 5
  2. 4
  3. 3
  4. 2
  5. 1
  6. ----do...while 使用-----
  7. 5
  8. 4
  9. 3
  10. 2
  11. 1

返回和跳转

  • return 默认从最直接包围它的函数或者匿名函数返回。
  • break 终止最直接包围它的循环。
  • continue 继续下一次最直接包围它的循环。

在循环中 Kotlin 支持传统的 breakcontinue 操作符
示例:

  1. fun main(args: Array<String>) {
  2. for (i in 1..10) {
  3. if (i==3) continue // i 为 3 时跳过当前循环,继续下一次循环
  4. println(i)
  5. if (i>5) break // i 为 6 时 跳出循环
  6. }
  7. }

结果:

  1. 1
  2. 2
  3. 4
  4. 5
  5. 6

Break 和 Continue 标签

在 Kotlin 中任何表达式都可以用标签(label)来标记。 标签的格式为标识符后跟 @ 符号,例如:abc@、fooBar@都是有效的标签。 要为一个表达式加标签,我们只要在其前加标签即可

  1. loop@ for (i in 1..100) {
  2. // ……
  3. }

可以用标签限制 break 或者continue:

  1. loop@ for (i in 1..100) {
  2. for (j in 1..100) {
  3. if (……) break@loop
  4. }
  5. }

标签限制的 break 跳转到刚好位于该标签指定的循环后面的执行点。 continue 继续标签指定的循环的下一次迭代。

标签处返回

Kotlin 有函数字面量、局部函数和对象表达式。因此 Kotlin 的函数可以被嵌套。 标签限制的 return 允许我们从外层函数返回。 最重要的一个用途就是从 lambda 表达式中返回

  1. fun foo() {
  2. ints.forEach {
  3. if (it == 0) return
  4. print(it)
  5. }
  6. }

这个 return 表达式从最直接包围它的函数即 foo 中返回。 (注意,这种非局部的返回只支持传给内联函数的 lambda 表达式。) 如果我们需要从 lambda 表达式中返回

  1. fun foo() {
  2. ints.forEach lit@ {
  3. if (it == 0) return@lit
  4. print(it)
  5. }
  6. }

它只会从 lambda 表达式中返回。通常情况下使用隐式标签更方便。 该标签与接受该 lambda 的函数同名。

  1. fun foo() {
  2. ints.forEach {
  3. if (it == 0) return@forEach
  4. print(it)
  5. }
  6. }

或者,我们用一个匿名函数替代 lambda 表达式。 匿名函数内部的 return 语句将从该匿名函数自身返回

  1. fun foo() {
  2. ints.forEach(fun(value: Int) {
  3. if (value == 0) return
  4. print(value)
  5. })
  6. }

当要返一个回值的时候,解析器优先选用标签限制的 return,即:

  1. return@a 1

意为”从标签 @a 返回 1”,而不是”返回一个标签标注的表达式 (@a 1)”。

END

发表评论

表情:
评论列表 (有 0 条评论,210人围观)

还没有评论,来说两句吧...

相关阅读