Previous episode: 24. Nested Loops & Early Exit
Next episode: 26. Conclusion
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.
Okay, so it's time for your next challenge. As always you can find the challenge in the zero nine 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. So, open it up and try solving the challenge questions on your own first but then keep watching and you can compare your work to mine. Good luck. (upbeat music) So, challenge number one. Write two loops One nested inside another that will print a nice 10 by 5 rectangle of asterisks like this. Okay I know you can do this much if you just bang away at the keyboard but, I want you to think, breathe and dream loops by the end of this video. So, let's construct the outer loop first. (typing) So, this will print our theoretical row of asterisks 5 times. Now, let's construct the inner loop. The one that will draw 10 asterisks. (typing) Okay, now I can use this really handy forum of the print statement to print an asterisk without going to the next line. (typing) Now the terminator here has nothing to do with Arnold Schwarzenegger this just means instead of poking that new line character on the end of my string put nothing there instead please. So, as my playground runs you can see I'm almost there but 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. (typing) And if I rerun my playground there, that fixes things. You can see my neat and tidy rectangle of asterisks in the console. Asterisks. That's a hard word to say. Asterisks, asterisks. Anyways, onto the next challenge. Challenge two. The array of pastries is back. Alright. So you're having a sale in your pastry shop but only on the pastries that are 5 characters in length or less. What a weird pastry shop. Okay, create a loop that will print out the pastries that are on sale. Okay, this looks pretty straight forward Now 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. (typing) Then let me set up the condition where I want to skip a pastry. if pastry count is greater than 5. (typing) Then I wanna stop whatever this loop is trying to do and tell it to go on to the next pastry. (typing) But, if I don't continue that is if the name of the pastry has 5 or fewer characters then print it out. (typing) Run my playground. And there you go. I guess just pies and donuts are on sale this week judging by the output. I guess I'll have to wait until next week to get some brownies. Challenge 3. So, the days of the week are back. Create a loop to print out just the days of the week from Monday through 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. Okay well I will start as always by creating my loop structure. (typing) Now I need an if statement to check if it's Sunday. (typing) And if it's Sunday I want to skip it. Now since Sunday only shows up once in this array I know this bit of code will only execute once. (typing) Then I wanna check if the day is Friday (typing) And if so, then break. (typing) Okay but, where should I put the print statement? If I put it here at the end (typing) and run my playground that's not quite right because Friday doesn't get printed out because of that 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. (typing) Rerun my playground. There, that's what I wanted. All of my favorite days of the week. That's the end of this challenge. Head on to the next video where I'll wrap up what you've learned in this section and I'll tell you what's up next. I'll see you there.
All videos. All books.
One low price.
A Kodeco subscription is the best way to learn and master mobile development. Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.