Learn Swift for Beginners 2026 - Lesson 11 - For Loops with Ranges
7:41

Learn Swift for Beginners 2026 - Lesson 11 - For Loops with Ranges

CodeWithChris 01.05.2026 383 просмотров 25 лайков

Machine-readable: Markdown · JSON API · Site index

Поделиться Telegram VK Бот
Транскрипт Скачать .md
Анализ с AI
Описание видео
In this lesson of the Learn Swift series, you'll learn how to use for loops with ranges — one of the most essential tools for writing clean, repeatable code in Swift. Resources: 📚 Full guide: https://codewithchris.com/learn-swift?utm_source=youtube&utm_medium=video&utm_campaign=learn_swift_lesson_11 Timestamps: 0:00 Introduction 0:20 Why use a for loop? 0:45 Writing your first for loop 1:32 Adding code inside the loop 1:58 Inclusive ranges explained 2:42 Using the loop variable (and underscore) 3:39 Stride: counting by custom steps 4:49 Challenge: multiplication table 5:57 Bonus: stride with even multiples 7:12 Wrap up & AI prompts You'll learn: ✅ What a for loop is and why it's useful ✅ How to write a for loop using closed ranges and half-open ranges ✅ How to use the loop variable i — or ignore it with an underscore ✅ How to use stride to count by custom steps, including counting down ✅ A hands-on challenge: building a multiplication table Whether you're brand new to coding or just starting out with Swift, this lesson breaks it all down step by step. 👉 Learn Swift playlist: https://www.youtube.com/playlist?list=PLMRqhzcHGw1bVghYq_TAiPeoKaBVlOJu5 #LearnSwift #SwiftProgramming #iOSDevelopment

Оглавление (10 сегментов)

Introduction

Hi, welcome back to our learn Swift series. Congratulations on getting this far because now we're into chapter three, repeating things. So, let's start with lesson one in chapter three here. This is four loops with ranges, and this is how we can write code that repeats something over and over again.

Why use a for loop?

For example, if you want to print out hello and you want to do this 10 times in a row, you could just actually print it out 10 times in a row like so, and you can copy and paste it 10 times. Or, you can just write the code once, and you can use this for loop to repeat it 10 times. So, let's take a look at how you write this for loop.

Writing your first for loop

Let's say we wanted to print this two times. We're going to write four. And then here you specify a variable. This keeps track of which loop number it's going to be or it currently is. You don't you need to use the keyword var that you would normally use to declare a variable. You simply write the variable name. And typically for these for loops, people like to use I. That stands for the index or the iteration of the loop. Then you use the keyword in and then you specify the range. So, this range of numbers is what you want to repeat or how many times you want to repeat. So, we're going to say let's go from one to two. And then we're going to put pair of curly brackets.

Adding code inside the loop

We're definitely going to want to put the print statement inside. Let's take a look at what happens in this case. So, it's going to print out twice. Notice that the difference between two and one is actually just one. 2 - 1 is 1. But it prints it out twice because this range is inclusive. So, it includes the lower bound and it includes the upper bound. So, the first loop

Inclusive ranges explained

I is going to be equal to 1. And then it's going to print it out like this. go and include this as well, two. On the second loop, I will be two, and then it's going to print this out. So, when you say 1 to 10 it will actually print it out 10 times. Now, what if you didn't want to include this upper range? Let's say you wanted to print from 1 to 9. You could do two dots and just a less than sign, and that would exclude the 10th iteration. What we can do is just put in the iteration number there. And you can see it goes from 1 to 9.

Using the loop variable (and underscore)

Now, this is also an example of using this variable I inside the loop. We're printing out hello with I. You can see how that affects the output here. But if we did not need to use this variable, we can in fact instead of actually declaring a variable, just put an underscore. And this tells Swift that I don't need to use the variable, so don't even declare one. And that works fine. Okay, let's take a look at what else we have here. So, we have um the range, which we talked about. This is called a closed range. It includes the numbers um the upper and the lower numbers. This is half open range. We talked about this with the less than sign. where we don't need the loop variable, so we just put an underscore.

Stride: counting by custom steps

underscore. And stride, yeah, we didn't talk about this, but this lets you count up or down by a number other than one. Because from here you can see that we're going 1 2 3 4 all the way up to 10. If we wanted to count by twos, let's say instead we use stride and it takes three uh three different numbers. So, from one through to 10 by two, let's say. Uh let's put the variable I back in so we can print it out here and we can see exactly what we're getting. So, yeah, we get 1 3 5 7 9, and you can count down as well. So, we can go from 10 to 1 counting by -1, for example. And what we get is starting from 10 all the way to 1. You can count down by twos and that's how you use stride. Let's take a look at the challenge now. And this is a multiplication table.

Challenge: multiplication table

Write a for loop that prints seven times table from 7 * 1 all the way up to 7 * 12. Each line should look like this. Okay. So, let's try this out. So, first of all, we are going to loop from uh one all the way up to 12. I open up a pair of curly brackets, and then we're going to print out um seven times and here we're going to substitute the variable I in there. All right, equals, and we're going to have to substitute the result as well. So, here we're going to actually put in the calculation 7 * I. And this will allow us to print out the seven times table just like that. So, let's ignore the hellos, but starting from here.

Bonus: stride with even multiples

There's a bonus part to this, which is to use a stride loop to print only the even multiples of seven. So, 7 * 2, 7 * 4 and so on. So, let's try that out ourselves here. Let's do that right here. So, instead of 1 to 12, we use the stride keyword. This one right here. Uh from we're going to do from two to 12 by two. And that will print out starting from here only the even multiples. Oh, but we wanted to include 12. So, there's a actually two different strides. I don't know if you saw there. This one uh that we used was two, but we probably want to use this one, which is through. Because this will actually include the end value. So, from two through 12 by two. So, that was a good thing that we did so that I could illustrate the difference between two and through. And now it includes 12.

Wrap up & AI prompts

Hope that was helpful. Don't forget you can do these AI prompts to extend this lesson and go a little bit further. So, this one right here is a quiz. gives you some more examples. And these prompts produce some code samples with comments that you can read and understand more. Again, the links to these prompts will be in the description below. All right, I hope this video was helpful, and I'll see you in the next one.

Другие видео автора — CodeWithChris

Ctrl+V

Экстракт Знаний в Telegram

Экстракты и дистилляты из лучших YouTube-каналов — сразу после публикации.

Подписаться

Дайджест Экстрактов

Лучшие методички за неделю — каждый понедельник