Leave a rating/review
Notes: 19. Challenge: While Loops
Update Notes: The student materials have been reviewed and are updated as of October 2021.
It’s time for your next challenge! You can find the challenge in the “03 - Challenge - While Loops” page of the playground you’ve been using, 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, then keep watching to compare your work to mine. Good luck!
Your first challenge is to create a loop that counts up from zero to nine. OK, so first, I’ll create a variable named count and set it equal to 0.
var count = 0
Next, you need to create a while loop with the condition count < 10.
while count < 10 {
Inside the loop, print out “Counting up: X” (where X is replaced with the count value):
print("Counting up: \(count)")
…and then increment count by 1, I’ll use the plus equals operator here:
count += 1
And the console shows a counting up from zero to nine. Good. Now, why are the final value of count, which is ten, and the value in the last line printed out, which is nine, different?
On the last pass through the loop, you print nine, and then increment count to ten. But then you get back up to the loop condition, determine that no, count is not less than ten, and the loop ends. That’s why count ends up at one higher than the value you printed out. On to 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 1, to one. OK, let me start by creating the repeat loop:
repeat {
}
Then I’ll print “Counting down: \(count)”:
print("Counting down: \(count)")
Then I’ll decrement count with the minus equals operator.
count -= 1
Finally, I need the loop condition at the end, while count is greater than zero:
} while count > 0
And there, the console counts down from ten, the current value of count, to one. Now, I could also use while count is greater than or equal to one:
} while count >= 1
There’s a third option, too: while count != 0:
} while count != 0
…but that one is a little dangerous. If you had a bug in your loop code that decremented count past zero, into negative numbers, this loop would then never ever end, and you’d end up with an infinite loop! Not what you want.
You’re going to build a dice simulator, that will continue to roll until you get a six. First, I’ll create a variable named rollCount and set it equal to 0.
var rollCount = 0
Create another variable named roll and set it equal to zero.
var roll = 0
Now I need to create a repeat-while loop:
repeat {
}
Inside the loop, to simulate the roll of a single die, I’ll set roll equal to the function Int.random(in: 1...6), which says “pick a random number between 1 and 6”.
roll = Int.random(in: 1...6)
Then increment rollCount by 1:
rollCount += 1
Finally, I’ll print out the result of roll and rollCount:
print("Roll \(rollCount) gives you a \(roll)")
Now, for the ending condition for the loop. I’ll set this to keep rolling while roll is not equal to six:
} while roll != 6
Now, you could argue that this theoretically could be an infinite loop, if you never got a six, but it’s pretty unlikely that a random number generator here will never deliver one of the values in this range within a reasonable number of tries.
And there - the console shows my rolls, until I hit six, and it stops. I can re-run the playground, and I should get a different set of rolls.
There you go! Your while loop skills are looking pretty good. Head on into the next video where you’ll learn about the next type of loop, known as a for-loop. I’ll see you there!