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

The while loop

A while loop executes a Boolean expression at the start of the loop, and the set of statements run until a condition becomes false. It is important to note that while loops can execute zero or more times. Here is the basic syntax of a while loop:

while <condition> { 
 // statement 
} 

Let's write a while loop in Playgrounds and see how it works. You have to add the following:

So, this while loop starts with a variable that begins at zero. Before the while loop executes, it checks to see whether y is less than 50, and, if so, it continues into the loop. Using the += operator, which we covered earlier, we increment y by five each time. Our while loop will continue to do this until y is no longer less than 50. Now, let's add the same while loop after the one we created and see what happens:

while y < 50 {
y += 5
print("y: \(y)")
}

When you are done, you should see the following:

You will notice that the second while loop never runs. This may not seem like it is essential until we look at our next type of loop.

主站蜘蛛池模板: 犍为县| 饶平县| 江西省| 赤城县| 石景山区| 金塔县| 新津县| 当涂县| 高青县| 乾安县| 广南县| 武平县| 襄垣县| 昭苏县| 莲花县| 六盘水市| 泗水县| 都安| 英山县| 台山市| 屏边| 屯昌县| 仙居县| 边坝县| 石家庄市| 金阳县| 阿拉善右旗| 伊吾县| 吉安市| 栾城县| 浦东新区| 德令哈市| 喀喇| 泉州市| 盐亭县| 泌阳县| 全州县| 伊金霍洛旗| 桦南县| 清涧县| 平山县|