Leave a rating/review
Notes: 25. Challenge: Nested Loops & Early Exit
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 “09 - Challenge - Nested Loops and Early Exit” 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!
Write two loops, one nested inside another, that will print a nice 10×5 rectangle of asterisks. OK, I know you could just do this by banging away at the asterisk key, but I want you to think, breathe and dream loops by the end of this video. So, let’s construct the outer loop first:
for _ in 1...5 {
This will print our theoretical row of asterisks five times. Now, let me construct the inner loop, the one that will draw ten asterisks:
for _ in 1...10 {
This is going to print ten asterisks for me. Now, I can use this handy form of the print statement to print an asterisk without going to the next line:
print("*", terminator: "")
The terminator here has nothing to do with Arnold Schwarzenegger; this just means “instead of poking that newline character on the end of my string, put nothing there instead, please!”
So as my playground runs, you can see that I’m almost there; I need each row of asterisks to print on a new line. I can force Swift to move to a new line with an empty print statement:
print()
…and that fixes things, so you can see my neat and tidy rectangle of asterisks in the console. Asterisks. That’s a hard word to say. Asterisks. Anyhow, on to the next challenge!
You’re having a sale in your pastry shop — but only on the pastries that are five characters in length or less. What a weird pastry shop! Create a loop that will print out the pastries that are on sale.
OK, this looks pretty straightforward. I have to keep in mind that I’m going to use continue to skip the ones that don’t match my condition. So let me start by creating the loop structure:
for pastry in pastries {
Then, let me set up the condition where I want to skip a pastry. If pastry count is greater than five:
if pastry.count > 5 {
Then I want to stop whatever this loop is trying to do and tell it to go on to the next pastry:
continue
But if I don’t continue, that is, if the name of the pastry has five or fewer characters, then print it out:
print(pastry)
There you go: I guess just pies and donuts are on sale this week, judging by the output in the console. I guess I’ll have to wait until next week to get some brownies.
Create a loop to print out just the days of the week from Monday to Friday, inclusive. Use continue to skip Sunday, and use break to exit the loop early once you encounter Friday - but make sure you still print “Friday”! OK! Well, I will start, as always, by creating my loop structure:
for day in daysOfTheWeek {
Now, I need an if statement to check if it’s Sunday:
if day == "Sunday" {
…then I want to skip it. Since Sunday only shows up once in this array, I know this bit of code will only execute once. Then, I want to check if the day is Friday:
if day == "Friday"
…and if so, break:
break
But where should I put the print statement? If I put it here at the end:
print(day)
…that’s not quite right, because Friday doesn’t get printed because of the break statement.
I want to print out the day just before I check if it’s Friday, because I do want to print it, but then I want to exit out when I hit Friday:
print(day)
There - that’s what I wanted! All of my favorite days of the week. That’s the end of this challenge - head on into the next video, where I’ll wrap up what you learned in this section and tell you what’s up next! I’ll see you there.