Hello will come again in this lesson you will be talking about four loops for loops are essentially
a way for us to repeat a certain task a certain number of times.
So if you want to do something 10 times or 20 times 100 times we use four loops for it.
And that helps us with a variety of situations.
Imagine if you're you know downloading the user from the backend and then our user has either 25 pictures
and you want to do this thing 25 times or any similar situation.
You will soon see that in every program that you write and every app that you write you use hundreds
of wardrobe simply because that scene or that's hard as as often as they are used.
They're very similar to if else statements are part of the control flow that let us do something and
then based on a condition they continue or they stop.
So just go ahead and see that in practice I'm going to jump into ex-cult.
I'm going to close the previous project I'm going to say is one is for loops.
Command line commands to
and they're essentially to put together a method for writing a four to one type of loop.
It runs based on a certain number of bytes ration.
So you say run it for 10 times or 20 times the other type tells you to run for based on the number of
elements in an array or a string or a number of elements inside a dictionary.
So on and so forth.
So let's see both of them.
The first one I'm going to say for ends by is 0 until a smaller than 10 plus plus run the ATI and then
every time I'm going to say it's like print Hello.
Let's first see how what the result it produces.
And then I'm going explain how it works.
So if this Opterons or this project runs now we should be able to see Hello hello hello hello hello
hello hello.
Ten times versing Hello.
So the way it's working is it saying there is a condition or must be below 10 and they're saying well
what does the I start from it says I ask thoughts from zero.
If it starts from zero is zero below 10.
Yes let's go and do this and then it says now add 1 to our.
So I becomes the one is the one below 10.
Yes it is.
Does this one keeps going on and on and on until let's say he is 8 and is saying is 8 is still below
10.
Yes it is then run this one says at one to it becomes nine the smaller And then this happens then nine
adds one to itself it becomes 10 is 10 below 10.
No it is not.
So this is not going to happen anymore more and we end up in this next line of whatever it is.
So this follow loop essentially happens and goes through all of its hydration until we get here.
Now what if I want to print from either or 25 to 80 I could say and Aslaug what whatever you find integer
type in itself thats an integer right.
So is going to print 25 to 79 like the other relational operators if you want to include 80 you could
just say until it is smaller or equal to 80.
So if you have an equal sign in there now 80 is going to be included in our in our iteration as a starting
gain from zero or two.
Let's say 10 and now this is going to happen 11 times.
I just want to say whatever that you are trying to do in here for 11 times which in this case is just
printing the eye.
You should also remember that it is again arbitrary.
You can name it whatever you like.
The same may in and then that becomes and as well.
And then this becomes an.
So we think that gain or we run again because I see the same thing a lot of times people actually call
these counters.
So between counter zero counters more than 10 counter plus plus Prange counter for the kind of game
we see the same result.
Now let me revert back to you which is a more common way of writing it.
I want to explain another thing for you.
This thing in here if you remember from one of the previous lessons this is incrementing I buy one.
So is going 0 1 2 3 until it is a smaller or equal 10 and then it gets to 11.
This fall finishes what people want it to go.
Every other one whatever if want it to go on lead to even numbers I would say plus equal to.
So instead of just adding 1 I would say I'd go to the to treat a treats for increments to every time.
Let's see that it should print 0 2 4 6 8 10.
Just like what we expected.
So that's another way.
Another thing to keep in mind that the patient doesn't have to go.
One other time even though most likely that is what you want and that's what we usually do.
You go 0 1 2 3 4.
We go in or there.
You could if you wanted to jump every turn to 1 or be 51.
If that's something that that you might need.
Now with that in mind let's move on to another type of 42.
So this what we call it a four new merge.
And now let's go ahead and do 4 in that see four in the works.
I think we saw something like this in one of our previous lessons because we needed to.
Yes.
When we were doing sets we saw that but let's see them once again and ask are they going to call this
one my array is on and this are a lock.
And in it with objects and the objects are let's say Wednesday and then there's another irrelevant object
called Max.
And then there is another object called by us.
And then there's another object called Friday and there's so many random objects that they seemingly
don't have any correlation with each other.
They don't have to have it inside the pause at the end.
We have a name that says this is the end of my Airey and I forgot the one bracket in here.
Like I said it's a very good practice if you stack them like this.
So you actually can see them quite clearly.
So what if I want it to go to the objects in this array.
So let's write that I'm going to say for instance is zero.
And these are.
It is a smaller.
So what should be a smaller from let's say in my array there is 1 2 3 4 5.
So I could see a smaller than five which obviously means zero.
One two three and four.
That's one way of doing it.
The other one would be my array doth count.
So it always depends on my array of course.
All right.
Plus plus increments.
And then you might incrementation in my area I'm going in my for loop I'm going to say and like are.
This is for instance or whatever you find.
What is it that we're looking for.
Why are they not like that.
I've checked that index.
I let me clear this one.
Or let me just comment it out and then I'll explain this once again.
Let me see what we get.
So this is Wednesday math Iowas Friday and Saturday so it's saying go between zero and the count of
the aere increment one at the time each time.
Print this part and whatever you find and then go to my array find the object that that index.
So if I 0 my array 0 if I use one my array one is eyes to my array to so on and so forth.
So this goes through the array and finds all of the objects.
However I told you I'm going to show you another way for doing it not in numeric for loop which you're
again doing in here so that other wave would be like this.
I'm going to say for NSA string any for each or whatever you want to call it in my.
It's means go to my array for objects of the type in the string go through them.
One other time and then place each of them as each call each of them each.
Or let's say in so it says go through my array.
One other time.
Call them and me.
And then what do you want to do with it.
I want to say and it's like whatever you find in any.
So it's saying go through the array one item at a time.
As long as there is a string one item at the time call them any so.
Annie.
ANNIE.
And any.
And then preinstall.
Let's see what we get if we do that.
If I run this application now we shouldn't get Wednesday Mantz I was Friday on Saturday exactly the
way that they are in here.
So that's about that.
A quick recap.
We do four loops and we want to repeat something a certain number of times we start from summer and
summer.
Remember what we want to include.
The last one or the one in the last one.
We can increment with different ways incrementing one other time when you're printing several other
time and you could use for in loops we could use it for a certain type of object in whatever object
we're looking for and then print the values that we want or do whatever we want to do with those values.
One of the things that we for instance we could do in here is we could say for any object in my array
Let's see if there is a Monday.
So I'm going to say for any in my array if any is equal to a string of Monday Monday.
If that is the case then and Aslaug Monday is there.
So if you're searching for a certain item you could do it like this and say go through this if it is
there printed.
Obviously Monday is not there now and he's not going to print it.
But if I was searching for oils anyway withdrawn this would say for any of them.
Yes.
Was is so perfect with the money.
Let's move on to our next lesson.