Welcome back and this video we are going to check out for loops, and they are used to iterate through
rangers' arrays, collections or anything that provides an iterator.
And you will learn about iterators, arrays, ranges and collections in the future.
Lecture's so no worries there.
But basically, it's just a set of values that are stored in one construct or in one type of data type.
All right.
As we've seen, there are multiple different data types such as and double strings and so forth.
But there are also data types which can store many values at once, and there are types of collections
or iterators.
All right.
So what we can do is create four loops as well in Scotland.
And a for loop is also very similar to a wire loop.
So we can just go ahead and use the four keyword and then num in one dot, dot 10 and execute some code.
And I'm just going to execute actually execute it in one line, just going to say print num in a string.
All right.
So this for loop here, which we have in three lines, so to say is the same thing as our wire loop
that we had here where we needed to create a variable.
Then we had the condition, then we did the execution of something or write something on the screen,
and then we needed to take care of the condition at one point not being met anymore.
And that's it.
All right.
But now we sum everything up so it's a lot shorter.
So here it just says for the number, which is in this range of one to 10, please execute the code
in the block.
And in our case, we just do something simple.
We just say, OK, print the number that you're currently looking at.
So if we run this, we can see that it's just prints one, two, three, four and so forth up to the
point where it's 10.
So what, the value is 10.
All right.
So this is just one way of writing a follow up.
There are other ways as well.
You can also, instead of saying dumb in and so you can say I in one until 10.
So that will pretty much give us the same result where we, for example, just print I and I'm going
to add an empty space here.
Let's run this.
And we can see that it printed the one, two, three, four, four and so forth from before from this
photo, and then we have this one empty space to empty space and so forth from this photo.
So here we just used this until keyword.
I would just say I want to have I in a range.
So this here is a range which says start at one and go up until 10.
Now it goes the other way around as well.
You've seen the keyword down too.
So please now go ahead and try to build a for loop, which goes from 10 down to one.
OK, I hope you tried it because we just used this key word already, so I thought would be a great
idea to use it in here is a little challenge.
So for I in 10 down to one.
All right.
And I'm just going to say that there should be a line in between so that we see that this is a down
to approach here that we had.
And that we are ten, nine, eight and so forth.
So what can you do if you want to have steps of not one, but let's say steps of two?
Well, you can simply add the keyword step in here and then say how big the steps should be.
So down to one with the steps of two who run this, then we will see ten, eight, six, four, two
and nothing else.
That's it.
So there is another way of writing this, what we have here.
So this down to approach and the general four to four loops.
So this is pretty much the same as doing the following as if we had this line here.
Dot step two, OK.
So either you write the of like this or you can also write the for loop like that where we just use
this slightly different approach.
All right.
But you can of course, do the same thing for our other four loops up there as well.
So for the second one, we we said I in one until ten, we can also say four.
I in one dot until ten.
Internally, the same thing will happen already.
So these are some examples, hopefully we're going to use them extensively, especially as soon as we
have collections, lists, arrays and all those kind of cool type of collections.
So see you in the next video.