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

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:

主站蜘蛛池模板: 武定县| 额尔古纳市| 沂源县| 宣化县| 溧水县| 平舆县| 广德县| 通道| 德安县| 孙吴县| 衡阳县| 土默特左旗| 柞水县| 铜梁县| 柳林县| 秭归县| 昌乐县| 奇台县| 玉林市| 凤台县| 寿阳县| 田东县| 黔西县| 洛隆县| 葫芦岛市| 理塘县| 车致| 阜南县| 黄冈市| 敖汉旗| 合肥市| 洞头县| 合山市| 宜宾市| 丹凤县| 报价| 光泽县| 大姚县| 宁阳县| 牙克石市| 临湘市|