English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this tutorial, you will learn to use break to terminate loops. In addition, you will also learn about break labels.
Assuming you are using a loop. Sometimes you may want to terminate the loop immediately without checking the test expression.
In this case, you can use break. When a break is encountered, it will terminate the nearest enclosing loop (without checking the test expression). This is similar toThe way the break statement works in Java.
It is almost always used withif..else together used. For example,
for (...) { if (testExpression) { break } }
If the if testExpression is evaluated to true, break will execute to terminate the for loop.
fun main(args: Array<String>) { for (i in 1..10) { if (i == 5) { break } println(i) } }
When running the program, the output is:
1 2 3 4
When the value of i equals5When, the value of i == 5When, if the value inside the if evaluates to true, then break is executed, which will terminatefor loop.
The following program calculates the sum of the numbers entered by the user until the user enters 0.
AccessKotlin basic input/outputto learn more about how to get input from the user.
fun main(args: Array<String>) { var sum = 0 var number: Int while (true) { print("Enter a number:") number = readLine()!!.toInt() if (number == 0) break sum +number } print("sum = $sum") }
When running the program, the output is:
Enter a number: 4 Enter a number: 12 Enter a number: 6 Enter a number: -9 Enter a number: 0 sum = 13
In the above program, the test expression of the while loop is always true.
In this case, the while loop runs until the user enters 0. When the user enters 0, break is executed, thereby terminating the while loop.
So far, you have learned about the break without a label, which terminates the nearest enclosing loop. Break can also be used in another way (labeled form) to terminate the required loop (which can be an outer loop).
Labels in Kotlin are prefixed withIdentifierStarting with, followed by @.
In this case, test@ is outsidein a while loopLabeled labels. Now, by using labeled break (in this case, break @test), it is possible to break out of a specific loop.
fun main(args: Array<String>) { first@ for (i in 1..4) { second@ for (j in 1..2) { println("i = $i; j = $j") if (i == 2) break@first } } }
When running the program, the output is:
i = 1; j = 1 i = 1; j = 2 i = 2; j = 1
Here, when i == 2When the expression evaluates to true, break@first is executed to terminate the loop labeled first@.
Here are some variants of the above program.
In the following program, break terminates the loop labeled @second.
fun main(args: Array<String>) { first@ for (i in 1..4) { second@ for (j in 1..2) { println("i = $i; j = $j") if (i == 2) break@second } } }
When running the program, the output is:
i = 1; j = 1 i = 1; j = 2 i = 2; j = 1 i = 3; j = 1 i = 3; j = 2 i = 4; j = 1 i = 4; j = 2
Note:Since break is used in the program to terminate the innermost loop, it is not necessary to use labeled break in this case.
In Kotlin there is3Structural jump expressions: break, continue, and return.