![iOS 12 Programming for Beginners](https://wfqqreader-1252317822.image.myqcloud.com/cover/76/36699076/b_36699076.jpg)
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:
![](https://epubservercos.yuewen.com/2E52D1/19470383901517806/epubprivate/OEBPS/Images/1000f4fe-dbc5-43d5-900f-19190be8e90a.png?sign=1738979100-yidvnUTiEF1v43tV7mNYnfBOiPvhgE55-0-f140cd2fde2fbd145937c45631e80a92)
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:
![](https://epubservercos.yuewen.com/2E52D1/19470383901517806/epubprivate/OEBPS/Images/3e9bb364-f595-4639-af78-8e4ec37b3a82.png?sign=1738979100-KS5jmT9YGFfzf4w6gONnIDAwHZZGwSFk-0-84cc604151f41c8e75fe55744ac3b2fc)
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.