Programming in Swift: Fundamentals

Oct 19 2021 · Swift 5.5, iOS 15, Xcode 13

Part 3: Control Flow

18. While Loops

Episode complete

Play next episode

About this episode

Leave a rating/review

See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 17. Introduction Next episode: 19. Challenge: While Loops

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Notes: 18. While Loops

Update Notes: The student materials have been reviewed and are updated as of October 2021.

Heads up... You've reached locked video content where the transcript will be shown as obfuscated text.

Now imagine a world where you had to write some Swift code to print out a string 100 times. It would be incredibly tedious to write 100 print statements, but that also violates the don't-repeat-yourself rule 99 times. Now, fortunately, Swift has a the tool to help with this, loops. In this exercise, you're going to try out one particular kind of loop, the While loop. Now a While loop syntax looks like this and they look an awful lot like an if statement. But the important difference is that as long as the condition of the While loop remains true, the code inside continues to execute. So the condition is checked, if it's true, the code executes, then the conditions checked again and so on. So you can see why it's called a loop. Now the loop ends when the condition is checked and it turns out to be false. So you'll start by writing a while loop. And then you're going to create a variant of a while loop called the repeat-While loop. And you'll learn the differences between the two. So download the materials for this section of the course, unzip them and open the Playground for this section. You'll find a page inside the Playground for each video of this part just like the Playground you used in parts one and two. Navigate to zero two While loops and let's get started. To start, let's create a While loop that will print out the numbers one through nine. You'll need two things, the While loop itself and the variable to track the number of times you've looped. So start with the variable. Call it i and set the value to one. it's not the most descriptive variable name to be sure, but you'll notice as you move along in your development career, that developers sometimes tend to use single letter variables, such as i J or K for simple loops. In a loop, i is a pretty well-known convention that means index. For a simple loop that counts up to nine i is enough. And now you're ready for the loop. Loop syntax starts with while then a condition. A condition is simply a Boolean expression and you should recall a Boolean expressions from previous parts of the course. In this case, you want to print out the numbers from one to nine. So you want the loop to run up to nine, but no further. Now the simplest way to say this is while i is less than 10. You could also say i is less than or equal to nine and that would mean the same thing. Now, finally, you need to tell Swift to print out the value of i each time it goes around the loop. To do that, you add a set of curly braces to hold the code to execute. And then inside those curly braces, you tell Swift to print i. Once you've printed the value of i, you need to increment i by one before the loop goes around again. Do you remember how to increment numbers? That's right, with the plus equals operator. Now, when the Playgrounds executes, you'll see the numbers one through nine printed in your console. Each time around the loop, Swift prints the current value of i and then increments i by one. When i is 10, the condition here is no longer true. So Swift stops executing the loop. Now what would happen if you declared i to be 10 before the loop started? Well, that's right, it wouldn't run. But what if you want it to guarantee that a loop would execute at least once before you even checked the condition for the loop? You can use a variant of the While loop called the repeat-While loop to do this. Now you may also hear this called a Do-While loop in some other languages, but in Swift, it's repeat-While. First we'll put a print statement in there so we can tell in the console where we start counting up again. And you also want to reset your counter to one so do that after that print line. Remember, you've declared this as a variable above, so you don't need to re-declare i down here. You just need to assign it the value you want. Now the syntax for a repeat-While loop is sort of backwards to the regular While loop. It starts with repeat followed by a set of curly braces with a loop code inside. So put the code you want to execute inside the curly braces Print i then increment i by one. Now, what condition are you going to end up with? You want to count up to nine, but remember this loop will execute first and then the condition will be checked. You want this to print the numbers up to including nine and then stop. So your condition will be i less than 10 again. That means when I is incremented to nine, the loop will run again, print nine, increment i to 10, and then the condition will be checked again and loop will stop. You can see in the console that your Playground counts up to nine and then counts up to nine again. Nice. Now to see the difference between these two loops, see what would happen if we set i to 10 before the loop started. So we'll change it in both spots where you assigned one to i and we'll assign it 10 instead. Here and here. Get my Playground again. Now you can see the difference between the two loops. With the While loop the condition is checked, but i is not less than 10 so this code never executes. The repeat-While loop however, executes the first time and prints out 10 and then checks the condition, finds the condition is false and doesn't repeat. So the important difference is knowing when the condition is checked. A While loop checks the condition before the loop code and repeat-While checks the condition after. And there you go. Two different flavors of While loops in Swift. And to make sure you really know the difference, I've got some challenges for you next that hopefully won't throw you for a loop.