In this lesson we are going to work on loops and let's start with a problem.
So imagine that we want to print something ten times.
Let's say I have my print hello.
And I want to do that ten times.
So the first uh, kind of obvious solution is you would just write this ten times.
So here just copy and paste ten times, okay.
Eight, nine and ten.
If I run this well we expect to have hello printed ten times and that's what we have.
So this works.
But it's very far from being optimal okay.
Writing the same code many times is really not efficient.
And it's quite a bad practice when programming okay.
The best is not to repeat yourself too much.
And also, what about if we want to print hello?
Not ten times, but 100 times or 1000 times?
Okay, you're not going to write this 1000 times.
In this case you are going to use Loops, so a loop will be used to repeat a given block of code.
Here, for example, the print.
Hello.
You only need to write the block of code once, and then it will be repeated as many times as it needs
to.
So I'm going to remove all of this for now.
And there are two main ways to write loops.
You can use a for loop and use a while loop.
I'm going to show you both here in this lesson.
And let's start with the for loop.
So I'm going to write a very simple for loop and then explain to you what it does.
Okay I'm going to start with the for keyword.
You can see we have the syntax highlighting for the for keyword.
And then I'm going to put I in range okay.
Open parentheses between zero comma and ten okay I put the colon I go back to a new line.
We have an indentation and I print.
Hello.
So that's the block of code that I want to execute.
Let's just run this to see.
And we have you can see.
Hello.
Printed ten times.
Okay.
So that's the exact same behavior as the previous one.
Now let me explain what I did here.
So first this line we have the for keyword okay.
That's going to create a for loop.
Then we have I here I is a variable.
It's an index.
So you create a local variable in this for loop usually for basic for loops.
Or when you want to go through an index or a counter.
We just use I as a convention okay.
You could use any uh, variable okay.
It doesn't really matter.
But we use I then you have the in keyword okay.
So for I in and here usually you will have a list.
We're going to see list in the next lesson.
So list or for example here a range that's the same thing.
So this range it just means that it's going to be between zero.
And so this value and then that value minus one okay.
So 0123456789.
And that's it.
So we have ten values between 0 and 9.
Then we have a colon.
And that's it for the first line of the file.
Okay.
So with this we have the definition of the for loop.
And inside you can see with an indentation we have the code that we want to repeat.
So what's going to happen is that this code is going to be repeated as many times as we have.
Well asked here which is basically a range between 0 and 10 minus one.
So we have ten values total.
Just one thing here I printed ten times.
But let's come back to our previous problem.
If I want to print it a thousand times, I just write 1000 here and I'm going to run this.
You can see it's taking a bit more time, because executing this 1000 times is not going to be instant.
And now we are done.
We have hello printed 1000 times okay, so you could count but no need.
Let's go back to ten and I'm going to talk about the indentation here okay.
You can see I have an indentation.
So the indentation is very important.
Now you can see if I go back to a new line I am still with an indentation, which means I am still in
the for loop.
I need to go back here to the previous indentation so that I am out of the for loop.
Let's just add a print end like this and let's run it.
And you can see we have hello ten times and then end.
All right.
So the end is executed after the for loop.
Okay.
So you can see the for loop has a loop going through this block of code.
And after ten times it's going out.
And then we print end.
If I put end like this you can see with an indentation even if we have an empty line here.
Okay.
Empty lines.
They don't do anything.
Look at what's going to happen.
You can see we have hello and hello and printed ten times okay.
Because this print is in the for loop because of the indentation.
If I remove the indentation the print is out of the loop.
So that's very important detail.
Okay.
Now let's come back to this AI variable that I created.
I told you that we create a local variable inside the for loop that's going to go through this range.
But we can actually print this.
So we can say hello.
And then plus and because it's going to be an integer number.
And now you know that we cannot concatenate a string with an integer number.
We need to cast this as a string just like that.
Make sure you add the correct amount of parentheses.
So I'm going to run this.
And now you can see we have hello zero.
And then hello 12345678 and nine.
I can add a space if I want to to make it a bit cleaner.
Okay.
You can see here clearly that we are going between 0 and 9.
So we have ten values total.
All right.
So that's how a for loop works.
Now there is another way to do what we did here using the while loop.
So let's see how that works.
I'm going to comment this code here.
So right click and toggle comment.
And I'm going to write a while loop I will keep this print n here.
And let's do the same with a while loop.
So first let me write the code and then I will explain it to you.
So here I'm going to create a variable I is equal to zero.
And then I will do while I is strictly lower than ten.
And then colon go back to a new line and we execute a block of code, which is this print that I'm just
going to copy here is the same.
So I copy and paste it right there.
And after that I will do I is equal to I plus one.
So while I is lower than ten strictly lower than ten we print hello with the I.
And then we add one to I.
And this is going to be run ten times.
Let's actually see Okay.
We have the exact same, you see.
Hello.
Zero until.
Hello, nine.
So this block of code is doing the same thing as that one.
Now let's see in detail how this is executed.
Okay.
So the while loop is actually going to test here.
You can see this is a condition okay.
So you can see the while actually has an if.
So it's an if with a loop.
Here you can read this as if I is strictly lower than ten.
So of course here we test a condition with a variable.
So that's why we have created the variable before okay.
In the for loop here you can directly create the variable inside the definition of the for.
But for the while we're going to create the variable before are we going to use another variable from
another place.
But here we create this variable here okay.
So if I is strictly lower than ten we're going to execute this block of code.
Then the difference is that with the if you just execute this once or not and then you go on the next
line here line number eight with a while after we have executed this.
So if this condition is true, we go back to line five and we test the condition again.
And as long as this condition is true, then we keep executing this block of code.
If the condition is false, then we go out of the while.
Okay.
So what's happening exactly we have I is equal to zero.
Then we test is I.
So is zero strictly lower than ten.
Yes.
So we go in the while we print hello zero.
And then we say that I is equal to its current value plus one okay.
So I becomes one.
Then we test again.
Here we go.
Back to line five.
Is one strictly lower than ten.
Yes we do the same thing.
And then I is equal to two.
We go all the way up to nine.
Then we print with nine and then nine.
So I becomes ten.
We go back here is ten strictly lower than ten.
No.
So we go out of the loop and then we print end okay.
So that's how it works.
And a very important thing here is you can see we have to uh, in this case we have to increment the
value ourself, okay.
Because if we don't do this well we're going to have what we call an infinite loop.
And that can be a problem because if you don't let's say comment this line, you see if you don't increment
I then I is always going to be zero.
And so this condition is always going to be true.
And so we are going to execute this an infinite amount of time.
And we are never going to go out of the while loop.
So that's why we add one to I here and here to write this.
Well there is an easier way to write that in Python.
If you just want to add a number to a variable instead of doing variable is equal to variable plus number,
you can just use plus equal like that okay.
If you do I plus equal one just means that you add one to I.
Okay.
So it's a bit better like this.
Great.
And now you have seen the for loop and the while loop, and the question is when to use a while loop
and when to use a for loop.
Okay.
Basically, you will use a while loop when you don't know how many times you need to execute the block
of code.
For example, if you read the temperature from a sensor and you need to keep executing action while
the temperature is below ten degrees, then you will need a while loop because you don't know how long
it will take for the temperature to rise above ten degrees.
So you need to continue until the condition that the temperature is greater than ten is met.
Now another example.
If you need to read the temperature a thousand times and then to compute the average temperature, then
you can use a for loop because you know exactly how many times you need to do the action.
Here it's 1000 times.
And so for the example we used here, because we know exactly how many times we need to print.
Hello.
We want to print hello ten times.
Then you can see that a for loop is a more appropriate choice.
And later in this course we will use both for and while loops for different purposes.