OK.
Welcome to another lecture.
We are now finishing up with our lectures on wildlife, and we are starting a lecture series on a new
type of loop, which is called a for loop.
So you might be thinking, well, why do we need another loop in a while, loop enough a loop as a loop,
right?
Well, technically, yes, you could use it while loops for everything, but there are certain situations
which for Loop is actually easier to use and kind of more sensible.
So take the below code simply.
For example, this is from our last program that we had in the last lecture, actually.
So you noticed that what we had to do was define a position variable set equal to zero an hour, while
Loop was kind of based on that right?
We said loop as long as position is less than five.
But to make sure that everything worked out, we had to do a plus plus position.
You can also do position plus plus.
The problem is, if we forgot to do this, somehow we forgot to put this last line in here, which is
easy, right?
Because you define your condition, you're like, I want it to loop as long as position is less than
five.
Here I do the stuff I care about.
And then you just put a close bracket and you forget this.
It's really easy to forget this line right here.
If we have to forget that the thing is that the loop would actually run infinitely.
So I will show you that infinite loop today and the editor.
You might have already seen that, though.
So I could just be showing you again in that case.
But the problem that that really arises is the reason why this is looking infinitely if we don't put
this is because think about it, position is equal to zero, right?
Well, if you look at this condition here, we're basically asking this year less than five, yeah,
zero is less than five, so we run this stuff in here.
Imagine this doesn't exist.
Position is still zero.
We go back to the top here and we ask is zero less than five we go down is zero less than five zero
less than five zero less than five zero less, but it just keeps going over and over again.
Zero is always less than five and position is always zero.
So always going to be true, always going to run the loop.
This condition is true and evaluates to true.
It's going to keep going and going and going and going.
Same thing for count.
If we did not increment count, it would just be like count less than five.
Well, yeah, count is zero zero less than five zero less than five zero less than five, and we just
keep doing that.
So if you had a lot of these loops in your program, it would be easy to make a mistake and get trapped
in an infinite loop.
And then you'd have to track that down.
And if your code has like thousands of lines and it's happening in multiple spots like you fixed one
infinite loop, then you had another one.
It can get kind of annoying.
It's just kind of a weird idea to have to put this in here if it was the only way.
Sure.
You know, you get used to it.
It's fine, but the fact is, is that we know how many times we want to loop.
The while it was kind of useful for when we want to just loop based on some condition, we're not really
sure how many times.
We need to loop, but we know for sure that we want to loop until something is like true or false.
You know, and tell us some condition is met kind of would be intel.
Whatever in here is evaluated to false because that's technically the syntax, right?
But it's kind of more based on just a condition, right?
Not not a certain amount of times, but this program we really cared about looping five times, right?
Because we didn't we went win position was zero and one position was one in position with two in position
with three in position was four.
So that was five times and then one position was five.
We did not run the code in here.
So we knew from the get go that we hardcoded this array to five and that we were going to want to loop
five times to get five numbers for each position of the array.
This is where a fault gets becomes a lot better solution.
So let's take a look at this.
A for loop is useful for running for a specific number of iterations, which is what our program was
doing right there.
So for loop is better.
So what we can do is that we can define the counter variable actually on the same line as the loop declaration,
which is pretty cool.
So you notice down in here, I have a for loop and right here rather than into position equals zero
like we had above the well loop.
We can put it in the same line actually inside the parentheses here.
And this doesn't have to be I.
It can also be position as we'll talk about here on the next slides.
We're going to break this piece of code down with these three components.
This one right here, this one right here and this one right here.
So first off, you want to use the word four instead of while you have the same parentheses as the while
loop, but it's going to have more stuff in it and then you'll also use the curly braces.
So this part right here, this is what I was talking about with the interposition equals zero.
So you don't have to use the character.
I this is really just to define our counter variable, just like the interposition equals zero and you
could have this equal and you can have this i b the word position instead.
So position here, position here, position here.
I sense for iteration, if you don't really know what to call the loop is kind of a de facto thing to
call it.
I that's just what a lot of programmers do.
But you know, you should call it whatever makes sense for the program.
So also note that you do not have to put this here, you can also put in A. equals zero above the for
loop and then just leave this section blank this underline section, except for the semicolon, and
then you would still have the rest of this in here.
So let's look at the rest of this.
What is the rest of this line?
So after finishing this first part where we declare the counter, so we start off at zero, start our
counter at zero.
We now need to say how long we're going to have the Lupron, how many iterations, what is this condition
based on the iterations?
Well, we need to use a comparison operator for this.
So what we're seeing right here is that I started zero.
The loop is going to run while I is less than size, so that's how many times it will run.
It will run from zero to size minus one, right?
Once in his size, it'll actually break out of the loop and continue on with the program, the rest
of the program.
So starting at zero, going up to but not including size.
So finally, we have to define what the increments will be.
So here we're just doing a simple A-plus plus, so that means we're incrementing I buy one each time.
You do not have to do A-plus, plus you do not have to just increment by one each time you can increment
by whatever you like.
You can use different operators in here and stuff.
So the last few things, like I said, are the curly braces inside of the curly braces is where the
magic happens.
That is where all of the stuff that you want to run multiple iterations should go right.
All the stuff that you want to be repeated multiple times for this amount of times.
Is going to happen where it says do stuff here.
And of course, that could be as many lines as you want.
Similar to the wallet.
So let's go ahead and head to the editor and modify that while the program to now instead use for loops.
So here I just kind of already written it out.
So what I'm going to do is kind of show you how this was.
So this was just a while and then we had position less than five, right?
That led us to have to have position equals zero here.
We also had to have an eight count equals zero.
And we have this + + position in the loop, but we don't need that anymore.
We definitely don't need this + + position because what we can do in here is just put this part right.
I copied pasted this here and then put how long the loop runs.
And then I put my position +, +, +, +, + + position here.
So here's the starting condition.
Right.
This is the start of this starting value for the iteration, for the variable cell position equals zero
goes from zero up until five and it stops at five.
So not including it's not going to actually run when position is five because it's positioned less than
five.
And then we have our increment right here on one line.
For the count, we had the same thing, except what we did now.
We don't need the count anymore.
There was a count plus plus in here or actually it was plus plus count.
We don't need that anymore because we have our.
Incrementing happening right here before all that was in here, it was actually just this count less
than five when it was a while loop instead of before.
Now we have count less than five as this middle piece.
We're starting at zero.
Just like this in position equals zero.
And that is our entire for loop.
You notice that our program is a lot shorter now, a lot more concise.
So this is something that you should be using when you know how many times you want to iterate over
something.
So specifically, we know the size of the array.
That's why we should, instead of while loop use a for loop, right?
So let's go ahead and just compile this so we can see what's going on.
I'm just going to leave it as well.
Loops to CBP.
It probably should be changed to four loops if I was actually going to hold on to this, but I go ahead
and compile it.
So let's go ahead and run this wild to see.
So you notice of yeah, I actually forgot to re say this then that a few times apologies for that.
So recompile re run it services.
Enter a number four position zero three three two two one five six.
And then you notice that they all print out nicely in there.
So, oops, I screwed up too much.
So let's scroll up just a bit so I can get them on here.
There you go.
So here we have the same exact results that we had before for the while loop, except we did it in a
much more clear and concise manner.
So here I entered three two one five six and it printed out three two one five six.
This part right here that I mistakenly had was from previous save right and we save, but that is something
that I wanted to show you, which is what happens with the infinite loop.
So let me go ahead and make another while loop here.
Just in case you haven't seen the infinite loop, I want to show you this.
So let's go back to this whole and position equals zero.
So what I can do is, I say, well positioned less than five, because that's what we had, right?
And then I'm going to go in here and I'm just going to say, see out, I'm in the side a loop and then
put it in line, so we initialize it to zero.
We say position less than five, but let's say we forget to do this classless position, you know,
or position plus plus whatever thing we're going to do.
And we just save it and run it.
So I save this.
Compile it, and I'm actually going to bring this up a little bit more.
We run it.
It just keeps saying over and over and over and over again, I'm inside a loop, I'm inside a loop,
I'm inside a loop and it kind of just flashing the same thing.
So you can't really see.
But like if I was to put, let's just say I did something like this, like I said, position minus minus.
And then I said, see out.
Position, you know, here I'm changing the value of position, but it's changing in the wrong direction,
we're still going to have an issue.
So let's see what happens when I do this.
It's a little funky there.
Let me be clear.
So looking at just counting down into negatives, just going and going and going and going and going,
so this shows you a little bit different output that really gives you a better idea of this infinite
loop that's just continuing to count down.
All right.
So that is why that is why sometimes the while loops can become sort of burdensome and it's better to
use a for loop if the situation makes sense.
If you know how many times you want to iterate, if you're iterating over some container that is specific,
known size, maybe not directly known by you, but you're able to call some sort of function that gives
you the size, anything like that, a good situation to use for that.
OK, so this was just meant to be a basic introduction to four lips.
The main things that I wanted you to take away from this are how to create a for loop what these things
are inside of here, just as a quick recap.
This is where the counter for the for loop starts.
This is the condition in which how long before Liverpool run for and this is the incremental version
of this counter for the for loop.
So with that, I will see you in the next lectures where we will practice for loops just as we practiced
while loops.