As like java break statement is used to break the iteration from the looping statements.
Kotlin Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | fun main(args: Array<String>) { for (i in 1..10) { if (i == 5) { break } println(i) } var sum = 0 var number: Int while (true) { print("Enter a number: ") number = readLine()!!.toInt() if (number == 0) break sum += number } print("sum = $sum") } |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | 1 2 3 4 Enter a number: 34 Enter a number: 13 Enter a number: 12 Enter a number: 2 Enter a number: 3 Enter a number: 3 Enter a number: 0 sum = 67 |