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

Next
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.

Transcript: 21. Challenge: For Loops

It’s time for your next challenge! You can find the challenge in the “05 - Challenge - For Loop” 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!

Create a for loop that counts by fives, up to and including 100. OK, I’ll start by crafting a for loop with a range of 1 to 100, inclusive:

for i in 1...100 {

}

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:

for i in 1...100 where i % 5 == 0 {

…and then I’ll print out the value of i:

print(i)

And there, the console counts up by fives, from five, all the way to 100! Nice.

Create a for loop that prints out a range of three numbers, the starting point of which you can control with a constant. Let me start by declaring the constant that controls the starting point of the range, here:

let rangeStart = 10

I’ve set it to ten, you can set it to something else if you like. OK, this is a simple extension of ranges. But instead of starting at one, you’re going to start at rangeStart:

for rangeValue in rangeStart

…and then you’re going to go to a value that’s three more:

for rangeValue in rangeStart..<rangeStart + 3 {

So since I have my rangeStart set to 10, this will go up to, but not including, 13. So this should print out 10, 11, and 12. Let me put in the print statement to make sure this is the case:

    print("Range value is \(rangeValue)")

And, as expected, the range goes from ten to twelve. If you like, change the constant to something else, to prove to yourself that the range always stays fixed to just three numbers.

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! Let me start with the random value for my range:

var randomCount = Int.random(in: 1...5)

I’ll create the structure of my loop next. I don’t care about the actual loop value, so I’ll ignore that with an underscore:

for _

…now, my range should go from one to the randomCount I defined above:

for _ in 1...randomCount {

And then, all I have to do it print my name out inside the loop, which I’m happy to do:

print("Chris")

I see my name printed out there in the console, and if I run the playground another time…I should see my name printed out a few more times.

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!