Almost done with the programming basics.
Now let's look at loops.
And again let's first understand why.
Here is a basic example that I'm going to run.
So loops.
And as you can see, this is quite stupid.
But we want to print hello ten times and you can see hello ten times.
And here it's stupid because we just need to write println.
Hello ten times.
What happens if I need to print hello a thousand times?
Well, whenever you need to repeat something more than two times, this would be best to use a loop.
So here I'm going to show you two kinds of loop.
The for loop and the while loop.
Let's first see how to use the for loop.
So I'm going to just remove all of that.
And let's use a for loop.
So you can start with four.
You can see the keyword for in green.
Then you open the parenthesis okay.
And then I'm going to write it and explain it just after.
So int I is equal to zero.
And then semicolon I lower than ten.
Semicolon I plus plus close the parentheses I open curly brackets, I press enter.
This automatically closes the curly brackets block and adds a two space indentation.
And then I can just put this right here.
Right.
And this let's let's run this.
This will do the exact same thing, but with just three lines of code.
Okay, you can see hello ten times.
So here a little bit more because we already had I'm going to clear the output.
Okay ten times.
We have already had data from the previous run on the serial monitor.
So.
Well, how to create a follow?
This is quite simple.
You have three different things you need to give in the parenthesis, separated by semicolons.
First is where you start.
So here I create.
You can see directly inside the int I create a variable.
So I declare it and initialize it.
So this will be the index we are going to use for the loop.
And I initialize it here to zero okay.
So that is the starting point.
And then the second stuff in the for loop is the condition for the loop to continue.
So here the condition is I is strictly lower than ten.
So basically as long as I is strictly lower than ten we are going to execute this block of code.
And the third parameter here is what we do with the variable with the index every time.
And here I do I plus plus I plus plus.
So I plus plus is the same as if you do I is equal to I plus one.
This will simply add one to the variable.
And this is specific to c and C plus plus.
And it's quite convenient.
So what we do we start at zero.
We check if zero is strictly lower than ten.
Yes we enter the four block and then we come back to this.
So what we do is we do I plus plus.
So now I is equal to one.
Is one strictly lower than ten.
Yes.
So we enter this block etc. etc. until we reach value ten.
So when I is equal to ten this condition will not be true okay.
Because ten is not strictly lower than ten.
And then we don't enter the block and we resume the program here on line seven.
And as you can see here, a particularity is that we start to count at zero.
Okay.
In programming, take this habit.
You don't start to count at one like you would do in real life, but you start to count at zero.
So to count from 1 to 10, you will actually count from 0 to 9.
Great.
And we are going to come back to this for loop quite a few times in this course because this is very,
very useful.
And now another loop you can use is while.
So I'm going to do the exact same thing we did here, which is to print that ten times but using a different
way with the while loop.
So first I'm going to create an int I.
So I'm creating an index okay.
And then the while basically while condition.
So while I is lower strictly than ten I'm going to open curly brackets.
And I'm going to do this.
So while I is strictly lower than ten I print.
Hello.
So here let's say hello four and hello while so we can make the difference.
And what I need to do is I plus plus okay.
And don't forget the semicolon after each statement.
Let's run that code here.
So I'm going to clear the output and upload.
Okay, you can see hello for ten times and hello while ten times.
So you can see the while is a little bit different because we just test one condition and then we go
inside the block.
So if you want to use a counter like we used in the for, you would need to create a counter here before
the while.
And then you of course need to increase the counter inside the while because if you don't increase it
well, I will always be lower than ten.
Okay.
If you don't increase I.
So this condition will just run indefinitely.
And this is what we call an infinite loop.
So an infinite loop is when you are stuck in one loop and you can't get outside of the loop.
So here, because we always increase by one, at some point I will be equal to ten and this condition
will be false.
So we continue.
So we resume the program at line 30 okay.
And here in the while we use a counter.
But you can actually use something different.
For example while while the push button is pressed do this.
And then once the push button is not pressed anymore we just leave the while.
Great.
So now when to use a for loop versus a while loop.
Basically, if you know how many times you need to go through the loop, use a for loop.
For example, if you need 100 samples from a sensor, then go with a for loop and use a while loop when
you don't necessarily know when you have to stop.
For example, if you need to read data from the same sensor but stop whenever the value reaches a certain
threshold.
So for the example of this lesson, we can clearly see that a for loop was the most appropriate, since
we knew exactly how many times we had to repeat the action.