官术网_书友最值得收藏!

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:

主站蜘蛛池模板: 木兰县| 随州市| 托克逊县| 巩义市| 大邑县| 田林县| 西华县| 鱼台县| 大埔县| 巨野县| 阿巴嘎旗| 岢岚县| 若羌县| 察雅县| 武隆县| 缙云县| 江都市| 招远市| 青田县| 历史| 鲁甸县| 陆河县| 泰来县| 汽车| 漳浦县| 潞西市| 菏泽市| 华蓥市| 疏勒县| 辽阳市| 多伦县| 清水河县| 深圳市| 临泽县| 铁力市| 崇仁县| 金塔县| 迁安市| 宜城市| 柘城县| 阿坝县|