Programming in Swift: Fundamentals

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

Part 3: Control Flow

21. Challenge: For 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: 20. For Loops Next episode: 22. Iterating Collections

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: 21. Challenge: For 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.

So, it's time for your next challenge. You can find the challenge in the 05 Challenge: For Loop Page of a 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, but then keep watching, as always, and you can compare your work to my work, good luck! (upbeat disco music) Challenge 1, create a for loop that counts by fives, up to and including 100. Okay, I'll start by crafting a for loop with a range of 1 to 100 inclusive. (keyboard keys click) But I don't want all 100 values, I just want all of the ones that are evenly divisible by five. So I'll use a where clause to limit this to just the numbers when divided by five have a remainder of zero. (keyboard keys click) And then I'll print out the value of i. (keyboard keys click) And if I execute my playground again? There, the console counts up by fives, from 5 all the way to 100. Nice! Challenge 2, Create a for loop that prints out a range of three numbers, the starting point of what you can control with a constant. Okay, let me start by declaring the constant that controls the starting point of the range here. (keyboard keys click) I've set it to 10, you can set it to something else, if you like. Okay, this is a simple extension of ranges. But, instead of starting at one, you're going to start at range start. (keyboard keys click) And then you're going to go to a value that's three more than a range start. (keyboard keys click) So since I have my range start set to 10, this will go up to, but not including 13. So this should print out 10, 11, and 12. So let me put the print statement in to make sure this is the case. (keyboard keys click) And if I execute my playground again, as expected, my range goes from 10 to 12. If you like, you change the constant to something else to prove to yourself that the range always stays fixed to just three numbers. Challenge 3! Create a for loop to print your name a random number of times between one and five. I love seeing my name written out as often as possible. So, let me start with the random value for my range. (keyboard keys click) I'll create the structure of my loop next. I don't care what the actual loop value is, so I'll ignore that with an underscore. (keyboard keys click) Now my range should go from one, to the random count I defined above. (keyboard keys click) And then, all I have to do is print my name out inside the loop, which I'm happy to do. (keyboard keys click) And, I run my playground again, and I see my name print in the console, twice. And if I run the playground at another time, it's printed out once. If I run it again, it's printed out five times. But that's it for this challenge. Come join me in the next video, where I'll show you a few more things that loops are good for, iterating collections. I'll see you there.