Programming in Swift: Fundamentals

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

Part 3: Control Flow

19. Challenge: While Loops

Episode complete

Play next episode

Next
About this episode

Leave a rating/review

See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 18. While Loops Next episode: 20. For 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: 19. Challenge: 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.

All right, It's time for your next challenge. So you can find the challenge in the zero three challenge, while loops page of the playground you've been using so far or, you can download a new one from the resources for this video, open it up and try solving the challenge questions on your own first but, then you can always keep watching to compare your solutions to mine. Good luck. (upbeat music begins) Okay. Your first challenge is to create a loop that counts up from zero to nine. Create a variable named count and set it equal to zero next, create a while loop with a condition count of less than 10. inside the loop, print out counting up X, where X is replaced with the count value and then increment count by one. Then explain why the final value of count and the value in the last line printed out are different. Okay, So first I'll create a variable named count and set it equal to zero. Next, I'll create a while loop with the condition count is less than 10. So inside the loop, I'll print out counting up X where X is the value of count, and then I'll increment count by one, I'll use the plus equals operator here. And if I execute my playground, the console shows I count up from zero to nine. That's good. Now, if I take a look at the value of count, and execute my playground again. Now, why is the final value of count, which is 10 and the value on the last line printed out, which is nine, different? On the last path to the loop, you print nine and then you increment count to 10, but then you get back up to the loop condition, determine that no, count is not less than 10 and loop ends. And that's why count ends up at one higher than the value you printed out. Onto the next challenge. Now that you've counted up, it's time to count down, build a repeat-while loop that will count down from the current value of count from challenge one down to one. first, print counting down X where X is the value of count inside the loop, then decrement count. And what are three different loop conditions you could use with this loop and get the same result. Okay, let me start by creating the repeat loop And then I'll print counting down and the value of count. And then I'll decrement count with the minus equals operator. Finally, I need the loop condition at the end while count is greater than zero, And I'll execute my playground again And there, the cancel counts down from 10, the current value of count down to one. Now I could also use while count is greater than or equal to one. And there's a third option too, while count is not equal to zero, but that one is a little dangerous. If you had a bug in your loop code, that decrement a count past zero into negative numbers, this loop would never, ever, ever end and you end up with an infinite loop and that is not what you want. Moving on. Challenge three, you're going to build a dice simulator that will continue to roll until you get a six, create a variable named roll count, set it equal to zero, create another variable named roll and set it equal to zero. Create repeat while loop, inside the loop, simulate the roll of a single die by setting roll equal to the function Int.random in one to six. And that just means pick a random number between one and six, then increment roll count by one. Finally, print roll X gives you a Y where X is the value of your roll count, and Y is the value of the roll you got. Set the loop condition so that the repeat while loop finishes when the first six is rolled. Okay, there's a lot to do here. So let's take it one piece at a time. So first I'm going to create a variable named roll count and set it equal to zero, then, I'll create another variable named roll and set it equal to zero. So now I need to create my repeat while loop. So inside the loop to simulate the roll of a single die, I'll set roll equal to the function Int.random in one to six, and this just means pick a random number between one and six, and then I'll increment roll count by one, and then finally, I'll print out the results of roll and roll count. Okay. Now for the ending condition for the loop. So I'll set this to keep rolling while roll is not equal to six. Now you could argue that this theoretically could be an infinite loop if you never got a six but, it's pretty unlikely that random number generator here will never deliver one of the values in this range within a reasonable number of tries. Let me execute my playground again. And there, the console shows my rolls until it hit six and it stops. I can re-run the playground then I should get a different set of rolls , so there you go. Your while loop skills are looking pretty good, head on into the next video where you learn about the next type of loop known as a for-loop, and I'll see you there.