Kotlin - Break and Continue

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.
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.
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 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

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.
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.
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 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

Q 1 - What is the purpose of the break statement in Kotlin?
A - Break statement is used to come out of the Kotlin program
B - Break statement is used to debug a Kotlin program
C - Break statement is used to come out of a loop
D - None of the above
Kotlin break statement is used to come out of a loop.
Q 2 - What is labeled expression in Kotlin?
A - Labeled expression are used to name different expressions in Kotlin
B - Labeled expression is an identifier followed by the @ sign
C - Labeled expression gives more control to jump the program execution
D - All of the above
All the given statements  about labeled expression are correct.
Q 3 - We can use continue statement to skip a part of the loop based on certain condition.
A - True
B - False
Yes it is true, continue statement is used to skip a part of the loop and to jump to the next available iteration.

    68 Lectures 
     4.5 hours 

 Arnab Chakraborty

    71 Lectures 
     5.5 hours 

 Frahaan Hussain

    18 Lectures 
     1.5 hours 

 Mahmoud Ramadan

    49 Lectures 
     6 hours 

 Catalin Stefan

    49 Lectures 
     2.5 hours 

 Skillbakerystudios

    22 Lectures 
     1 hours 

 CLEMENT OCHIENG
 Print
 Add Notes
 Bookmark this page