KOTLIN: While Loop

From OnnoWiki
Revision as of 11:24, 25 July 2022 by Onnowpurbo (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

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


Kotlin while loop executes its body continuously as long as the specified condition is true.

Kotlin while loop is similar to Java while loop.


Kotlin while loop mengeksekusi tubuhnya secara terus menerus selama kondisi yang ditentukan benar.

Perulangan while Kotlin mirip dengan perulangan while pada Java.


Syntax

The syntax of the Kotlin while loop is as follows:

Sintaks dari loop while Kotlin adalah sebagai berikut:


while (condition) {
    // body of the loop
}

When Kotlin program reaches the while loop, it checks the given condition, if given condition is true then body of the loop gets executed, otherwise program starts executing code available after the body of the while loop .

Ketika program Kotlin mencapai while loop, ia memeriksa kondisi yang diberikan, jika kondisi yang diberikan benar maka body loop akan dieksekusi, jika tidak program mulai mengeksekusi kode yang tersedia setelah body loop while .


Example

Following is an example where the while loop continue executing the body of the loop as long as the counter variable i is greater than 0:

Berikut adalah contoh di mana while loop terus mengeksekusi tubuh loop selama variabel counter i lebih besar dari 0:


fun main(args: Array<String>) {
   var i = 5;
   while (i > 0) {
      println(i)
      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:

5
4
3
2
1

Kotlin do...while Loop

The do..while is similar to the while loop with a difference that the this loop will execute the code block once, before checking if the condition is true, then it will repeat the loop as long as the condition is true.

The loop will always be executed at least once, even if the condition is false, because the code block is executed before the condition is tested.


Do.. while mirip dengan perulangan while dengan perbedaan bahwa perulangan ini akan mengeksekusi blok kode satu kali, sebelum memeriksa apakah kondisinya benar, maka akan mengulangi perulangan selama kondisinya benar.

Loop akan selalu dieksekusi setidaknya sekali, bahkan jika kondisinya salah, karena blok kode dieksekusi sebelum kondisi diuji.


Syntax

The syntax of the Kotlin do...while loop is as follows:

Sintaks dari loop do... while Kotlin adalah sebagai berikut:


do{
    // body of the loop
}while( condition )

When Kotlin program reaches the do...while loop, it directly enters the body of the loop and executes the available code before it checks for the given condition. If it finds given condition is true, then it repeats the execution of the loop body and continue as long as the given condition is true.

Saat program Kotlin mencapai loop do... while, program tersebut langsung memasuki tubuh loop dan mengeksekusi kode yang tersedia sebelum memeriksa kondisi yang diberikan. Jika menemukan kondisi yang diberikan benar, maka ia mengulangi eksekusi tubuh loop dan melanjutkan selama kondisi yang diberikan benar.


Example

Following is an example where the do...while loop continue executing the body of the loop as long as the counter variable i is greater than 0:

Berikut adalah contoh di mana perulangan do... while terus mengeksekusi badan perulangan selama variabel penghitung i lebih besar dari 0:


fun main(args: Array<String>) {
   var i = 5;
   do{
      println(i)
      i--
   }while(i > 0)
}

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:

5
4
3
2
1

Quiz Time (Interview & Exams Preparation)

Q 1 - Which of the following is a loop statement in Kotlin?

A - for

B - while

C - do...while

D - All of the above

Q 2 - What is difference between while and do...while loops?

A - while loop is faster than do...while loop

B - do...while loop is faster than while loop

C - while loop checks condition before executing its body, where as do...while checksthe condition after executing its body atleast once.

D - There is no difference between two types of loops


Referensi