Hello and welcome to this late show on for loops a for loop is basically a repetition control structure
so there are times you may want setting codes to run repeatedly within an application.
For example you can have an application that's called several arrays.
You may want the number you may want a mechanism or a structure that all makes setting codes execute
by iterating true the arrays or some parts of the application.
So the for loop can be used as that kind of repetition control structure.
OK.
So with the for loop you can specify how many times you want a piece of code or a loop to execute.
You can specify the number of times you want the loop to execute the syntax syntax basically refers
to the structure of a loop how you define the loop.
So this is how you basically to find the loop.
You start with the word for and then he takes in three parameters.
So this is basically the control flow of the loop.
So you've got the first condition which is the initial.
And then you've got the condition.
This in this one is this step is executed first and only once then you've got the condition.
All right.
The condition is evaluated if it is true the body of the loop is executed if it is false the body of
the loop does not execute and the flow control jumps to the next statement after that loop.
This the increment after the body of the after the body for the for loop was that is executed.
The increment the flow control jumps back to the increment statement and then allows you to update any
loop control within that with any control variable within the loop some someone to illustrate this with
a.
So that makes more sense.
So basically you specify a loop like that and then whatever statement or block of course you need to
write goes in between this curly braces.
So let me illustrate that this is a flow diagram of how the loop process works.
So this is the syntax here for the loop and this is where the conditional statement goes.
So basically the first first step in the loop is the inmate this inmate here.
So this step this step here is executed first and only once.
So this step allows you to declare and initialize any loop control variables you are not required to
put a statement here.
You don't need to put any statement in this part.
OK.
As long as the semicolon appears OK.
This is the semicolon is longer the semicolon appears that you don't need to pull any.
All right.
So the next is the condition which is this condition here.
That's the condition.
So if the condition set is true it will execute this code block all of our conditional state code you've
written here that code will execute if the condition is false.
The code will execute here.
All right.
So if the condition set once the.
The code has been evaluated if the conditions set is true the body of the loop which is this code block
here the body of the loop is executed if it is false the body of the loop does not execute and the flow
control jumps to the next statement after the loop.
OK.
So it would jump to the next statement after the Look if you've got several statements set up after
the body for the loop executes the flow control jumps back up to the increments statement which is the
statement yet this is increment it jumps back up to the increment statement this statement allows you
to update any loop control variables.
So this statement can be left blank as long as a semicolon appears after the condition this condition.
All right.
So the condition is now evaluated again and if it is true the loop will execute and the process repeats
itself.
Body of the loop then increment.
That is the body of the loop.
Execute.
Then the increment.
All right.
And this process keeps on going again.
Once the condition is tested if it's true the loop continues until the condition becomes false and then
the loop terminates.
All right.
That's basically how a loop works.
So let's try and implement that quickly if we can I have created a basic example here of the for loop
so what I've done have created a console app.
So the way you create a console log basically you go to file new projects make your C sharp is selected
and then select console application and then give your console a name and then you click Okay.
Once you click Okay this code here will be generated for you.
This bit will be generated.
The only code I have written is this code here.
One highlighter so the other the other parts of the code will be auto generated.
So between this code here this opening one here and this one here is where you write your code.
So this is where I have written my code
between this two curly braces.
This one this one.
Let me explain the code to you here.
So with a for loop there are three parts of the code.
The first part of the code is the initialization where you initialize it.
So that's it here.
I've created a variable called X. given its value of 17 so this is the first part.
The initialization second part is a condition I've set I've set a condition here saying as long this
I want the code to lose value.
This code to execute as long as X is less than twenty seven.
So that's the condition.
And then here is the increment.
So I want X to increase by 1 each time until it gets to a value where it is less than twenty seven.
So this is the code here that will execute.
So it will display on the screen it will show this text the value of x.
It says This here is the initialize.
So the first value will be zero before the counting starts.
So once this code is executed this value will not change until it gets to 26.
Remember the condition here is that X has to be less than twenty seven.
So we will expect the value to be displayed each number to be displayed on the screen from 17 to 26.
OK.
And this console the red line is basically code basically to enable the code that you execute to stay
on the screen.
So let's check that out.
So we'll click on Start and as you can see here the code is executing.
So the condition here is that E2 it would do.
Each would keep displaying the numbers of the value of X on till it gets to 26.
So as long as this is less than twenty seven so twenty six would be the highest.
So that means we could keep executing seventeen sixteen and so on until it gets to twenty six.
So that's basically how you implement a for loop.
All right.
I hope it has made sense if it hasn't.
Please feel free to contact me or be more than happy to explain more.
Take care.
Bye for now.