KOTLIN: Break Continue

From OnnoWiki
Jump to navigation Jump to search

Sumber: https://www.tutorialspoint.com/kotlin/kotlin_break_continue.htm

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.

Pernyataan break Kotlin digunakan untuk keluar dari loop setelah kondisi tertentu terpenuhi. Perulangan ini bisa berupa perulangan for, while atau do... while.


Syntax

Let's check the syntax to terminate various types of loop to come out of them:

Mari kita periksa sintaks untuk menghentikan berbagai jenis loop untuk keluar darinya:


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

Jika ekspresi pengujian dievaluasi menjadi true, pernyataan break dieksekusi yang mengakhiri loop dan program terus mengeksekusi tepat setelah pernyataan loop.


Example

Following is an example where the while loop terminates when counter variable i value becomes 3:

Berikut ini adalah contoh di mana while loop berakhir ketika nilai counter variabel i menjadi 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:

Ketika Anda menjalankan program Kotlin di atas, itu akan menghasilkan output berikut:

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.

Label Kotlin adalah bentuk pengenal yang diikuti dengan tanda @, misalnya test@ atau outer@. Untuk membuat Ekspresi Kotlin apa pun sebagai berlabel, kita hanya perlu meletakkan label di depan ekspresi.

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

Pernyataan break berlabel Kotlin digunakan untuk mengakhiri loop tertentu. Hal ini dilakukan dengan menggunakan ekspresi break dengan tanda @ diikuti dengan nama label (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:

Ketika Anda menjalankan program Kotlin di atas, itu akan menghasilkan output berikut:

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.

Pernyataan continue Kotlin memecah iterasi loop di antaranya (melewati bagian di sebelah pernyataan continue hingga akhir loop) dan melanjutkan dengan iterasi berikutnya dalam loop.

Syntax

Let's check the syntax to terminate various types of loop to come out of them:

Mari kita periksa sintaks untuk menghentikan berbagai jenis loop untuk keluar darinya:

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

Jika ekspresi pengujian dievaluasi menjadi true, pernyataan continue dijalankan yang melompati bagian loop yang tersisa dan melompat ke iterasi berikutnya dari pernyataan loop.

Example

Following is an example where the while loop skips printing variable i when its value is 3:

Berikut ini adalah contoh di mana while loop melewatkan variabel pencetakan i ketika nilainya 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:

Ketika Anda menjalankan program Kotlin di atas, itu akan menghasilkan output berikut:

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

Pernyataan continue berlabel Kotlin digunakan untuk melewati bagian dari loop tertentu. Hal ini dilakukan dengan menggunakan ekspresi continue dengan tanda @ diikuti dengan nama label (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:

Ketika Anda menjalankan program Kotlin di atas, itu akan menghasilkan output berikut:


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

Quiz Time (Interview & Exams Preparation)

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

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

Q 3 - We can use continue statement to skip a part of the loop based on certain condition.

A - True

B - False


Referensi