All right.
So let's start with for loops.
And therefore I'm going to create a new console application that I'm going to call loops basics.
All right.
And let's click next.
We're going to leave everything by default for the framework here dotnet eight Long-Term support is
perfect for us here okay.
So let's get rid of our console red line statement here and create our first for loop.
So we are going to use the for keyword.
And you can see that there is this code snippet that we can directly use.
And let's just go over it and press the tab key.
And you can see that it automatically gives us this for loop structure.
So we didn't even have to type it in ourselves.
So what you can see is that there is an error still with this length okay.
So we need to define how long or for how many times the iteration should run.
So if I enter ten it will run ten times because it starts at zero.
And zero is included and then it goes until less than ten.
So this means that it will go from 0 to 1 234567, eight nine.
And that's in total ten iterations.
So therefore I'm just going to use a console write line statement here.
And I'm just going to say what I is.
So I is and then I'm going to add plus I to keep it very simple.
So let's run this code.
And let's also make sure that our program is not closing.
So we're going to use the read key here.
And there we are.
It says I is zero I is one I is two I is three and so forth.
So it ran the code ten times in total.
And this is pretty cool.
So that means that now we can do whatever we want multiple times.
So we can run our same code ten times instead of having to do it manually, where let's say we would
count and add, we would say I is zero.
Then we would say I is one and so forth.
Okay, we don't have to do that anymore.
Like that would be something which we would have done in the past where we wanted to run a code multiple
times, and this would have been the way how we would have to do it, type it in manually, and it's
really a pain.
But the cool thing now is that we can just use a for loop.
So a loop that runs the same code as long as we want it to run.
So I is just the default name.
We could have replaced this name with counter.
You can also rename it here and call it counter for example okay.
So if we call it counter maybe it's a little easier to understand.
So this one will be counter is.
So this is just a variable name that we define here.
And we assign a value to it.
So this is an example where we started at zero.
But we can also start at five okay.
So if I run this you can see counter is 56789.
Now what if I wanted the tend to be included.
Then there are two ways.
Either I enter 11 or I say less than equal ten.
Okay, this will now make sure that the ten is included as well.
So what is going on here?
Because well, it's cool that we can run this code multiple times.
But what is technically happening now, we can debug this as well to see this in action.
Okay.
So let me run this okay.
So we see counter starts with zero.
But then once we go into we see counter turns to five okay.
So now it goes into the second statement because these are each statements right.
There is a semicolon after that.
So when you have a semicolon basically you're closing or finishing the current statement.
So this first statement is like setting up a variable called counter and set it to five.
Then the second statement is checking if counter is less than ten.
So this statement here is set to true.
You can highlight this and press shift and F9 and you will see that this statement is true.
So counter is less equal ten is true.
So we get into the next step where it runs this code.
So it gets here.
The next step is to execute this line of code.
The next step is to go to the curly brackets here.
And you will see.
Then it goes to this counter.
So that's what I just explained.
And now you see how it works with your debugger.
So now it goes over to checking again.
Now counter is six.
You see the value here of counter now is six six less than ten or less than equal ten.
Again shift F9.
We can see this statement.
It is true.
So counter less equal ten is true.
So now let me run it a few times.
Here we see there at the bottom counter is now seven and it runs.
The code counter is eight, counter is nine and our counter is ten.
So in the next iteration there we are counter is ten.
And now it runs the code one more time.
Executing counter is ten.
And then once it's here it's incrementing counter to 11.
And then in the next iteration we see counter.
Now is 11.
Now if you go over this piece here again shift F9, you can see that the statement is false.
So this bool so to speak is false.
And if we go to the next line you can see it directly jumps to the next executable line which is line
nine.
And that's how a for loop works.
Very simple okay.
In the next video I want to show you a count down for loop because this is just a count up for loop,
right?
But we can also count down.
So let's go ahead and see that in the next video.