The while loops

A while loop executes instructions within a block as long as a specified condition is met. The while loops are created using the while keyword. It takes the following form:

while (condition) { … }

As in the case of the for loop, the block is optional in the case where only one sentence is within the scope of the loop. In a while loop, the statements in the block execute repeatedly while the condition specified still holds. Consider the following code:

val names = arrayOf("Jeffrey", "William", "Golding", "Segun", "Bob")var i = 0while (!names[i].equals("Segun")) {  println("I am not Segun.")  i++}

In the preceding program, the block of code within the while loop executes and prints I am not Segun until the name Segun is encountered. ...

Get Kotlin Programming By Example now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.