In this guide, we will discuss Break and Continue in Kotlin.
Kotlin Break Statement
Kotlin break statement is used to come out of a loop once a certain condition is met. This loop could be a for, while or do…while loop.
Syntax
Let’s check the syntax to terminate various types of loop to come out of them:
// Using break in for loop for (...) { if(test){ break } } // Using break in while loop while (condition) { if(test){ break } } // Using break in do...while loop do { if(test){ break } }while(condition)
If test expression is evaluated to true, break statment is executed which terminates the loop and program continues to execute just after the loop statment.
Example
Following is an example where the while loop terminates when counter variable i value becomes 3:
fun main(args: Array<String>) { var i = 0; while (i++ < 100) { println(i) if( i == 3 ){ break } } }
When you run the above Kotlin program, it will generate the following output:
1 2 3
Kotlin Labeled Break Statement
Kotlin label is the form of identifier followed by the @ sign, for example test@ or outer@. To make any Kotlin Expression as labeled one, we just need to put a label in front of the expression.
outerLoop@ for (i in 1..100) { // ... }
Kotlin labeled break statement is used to terminate the specific loop. This is done by using break expression with @ sign followed by label name (break@LabelName).
fun main(args: Array<String>) { outerLoop@ for (i in 1..3) { innerLoop@ for (j in 1..3) { println("i = $i and j = $j") if (i == 2){ break@outerLoop } } } }
When you run the above Kotlin program, it will generate the following output:
i = 1 and j = 1 i = 1 and j = 2 i = 1 and j = 3 i = 2 and j = 1
Kotlin Continue Statement
The Kotlin continue statement breaks the loop iteration in between (skips the part next to the continue statement till end of the loop) and continues with the next iteration in the loop.
Syntax
Let’s check the syntax to terminate various types of loop to come out of them:
// Using continue in for loop for (...) { if(test){ continue } } // Using continue in while loop while (condition) { if(test){ continue } } // Using continue in do...while loop do { if(test){ continue } }while(condition)
If test expression is evaluated to true, continue statment is executed which skips remaning part of the loop and jump to the next iteration of the loop statment.
Example
Following is an example where the while loop skips printing variable i when its value is 3:
fun main(args: Array<String>) { var i = 0; while (i++ < 6) { if( i == 3 ){ continue } println(i) } }
When you run the above Kotlin program, it will generate the following output:
1 2 4 5 6
Kotlin Labeled Continue Statement
Kotlin labeled continue statement is used to skip the part of a specific loop. This is done by using continue expression with @ sign followed by label name (continue@LabelName).
fun main(args: Array<String>) { outerLoop@ for (i in 1..3) { innerLoop@ for (j in 1..3) { if (i == 2){ continue@outerLoop } println("i = $i and j = $j") } } }
When you run the above Kotlin program, it will generate the following output:
i = 1 and j = 1 i = 1 and j = 2 i = 1 and j = 3 i = 3 and j = 1 i = 3 and j = 2 i = 3 and j = 3
Next Topic : Click Here
Pingback: Kotlin - While Loop | Adglob Infosystem Pvt Ltd
Pingback: Kotlin - Control Flow | Adglob Infosystem Pvt Ltd