English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this tutorial, you will learn how to use 'continue' to skip the current iteration of a loop. Additionally, you will also learn about the 'continue' label in this article.
Assuming you are using a loop. Sometimes you may want to skip the current iteration of the loop.
In this case, use 'continue'. The 'continue' construct skips the current iteration of the enclosed loop, and the program control jumps to the end of the loop body.
它几乎总是与it is almost always used with if ... else
constructs are used together. For example,1) { // if (testExpression1 while (testExpression2) { continue } // if (testExpression2 }
codes2If the testExpression
fun main(args: Array<String>) { for (i in 1..5) { println("$i Always printed.") Example: Kotlin continue 1 if (i > 5) { continue } The continue will be executed, thus skipping the execution } }
When running the program, the output is:
1 && i < 1 Always printed. 2 && i < 3 && i < 4 && i < 5 && i < 5 Always printed.
Not always printed.1when the value of i is greater than5and less than
The continue will be executed, thus skipping the execution
statement is declared:
But, when
println("$i Always printed.")
in each iteration of the loop, because this statement exists before the continue construct.
The following program calculates the maximum6the sum of positive numbers. If the user enters a negative number or zero, it will be skipped from the calculation.
AccessKotlin basic input/outputto learn more about how to get input from the user.
fun main(args: Array<String>) { var number: Int var sum = 0 for (i in 1..6) { print("Enter an integer:") number = readLine()!!.toInt() if (number <= 0) continue sum += number } println("sum = $sum") }
When running the program, the output is:
Enter an integer: 4 Enter an integer: 5 Enter an integer: -50 Enter an integer: 10 Enter an integer: 0 Enter an integer: 12 sum = 31
So far, what you have learned is the continue without a label, which skips the current iteration of the nearest enclosed loop. continue can also skip the iteration of the required loop (which can be an outer loop) by using the continue label.
Labels in Kotlin are marked withIdentifierbeginning, followed by @.
Here, outerloop@ is atwhile loopExternal labeled labels. Now, by using the continue label (continue@outerloop), you can skip the execution of the specific loop code in the current iteration.
fun main(args: Array<String>) { here@ for (i in 1..5) { for (j in 1..4) { if (i == 3 || j == 2) continue@here println("i = $i; j = $j") } } }
When running the program, the output is:
i = 1; j = 1 i = 2; j = 1 i = 4; j = 1 i = 5; j = 1
It is usually not recommended to use labeled continue, as it can make your code difficult to understand. If you must use labeled continue, please refactor the code and try to solve it in other ways to make it more readable.
There are3structured jump expressions: break, continue, and return.