- Kotlin Programming By Example
- Iyanu Adelekan
- 153字
- 2021-08-27 20:00:11
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 = 0
while (!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. Once Segun is encountered, the loop terminates and nothing else is printed out, as shown in the following screenshot:

推薦閱讀
- 案例式C語言程序設計
- Computer Vision for the Web
- SpringMVC+MyBatis快速開發與項目實戰
- Python數據分析基礎
- ASP.NET Core Essentials
- C語言程序設計(第2版)
- Oracle 12c中文版數據庫管理、應用與開發實踐教程 (清華電腦學堂)
- 深入RabbitMQ
- Mastering JavaScript Design Patterns(Second Edition)
- Learning ArcGIS for Desktop
- Mastering React
- INSTANT Yii 1.1 Application Development Starter
- Python Essentials
- Hands-On Neural Network Programming with C#
- Visual FoxPro 6.0程序設計