We’re going to use a keyword for and and between these parentheses we need to write the expressions or the statements which are responsible for the working of this for loop and between these curly braces we can write the statements that we want to execute so here the first statement inside these curly braces is going to be the initialization and this initializing statement is nothing but we’re going to create a variable which is called loop counter and we’re going to set the initial value of that counter variable here and next .
we need something called condition so every time this condition is checked before running the statements inside this loop if this condition holds good then the statement will be executed and of this condition fails or this condition yields false value then this statement will not be executed and the next one is called updating okay and this initialization and condition and operation are separated by the semicolons .
so don’t get confused here you know when we use this in our program it will be clear all right now we’re gonna see how to use this for loop in our program so as I told you in the while loop tutorial you know if you guys wanted to print some message.
Let’s say 30 or 50 times at that time you can’t really write the statements 30 or 50 times what you guys can do is you guys can write the statement only once and make the compiler to execute the statement you know as many times that you so here for the demonstration purpose.
I’m going to create a variable and I’m going to call it as counter and we’re going to initialize it later and next we’re going to write the follow-up so it’s gonna be for keyboard and then pair of parentheses and then pair of curly braces and between this curly braces our statement will be that and here inside this parenthesis we need to write the initialization so this initialization is nothing.
But initializing the loop counter variable we’re going to initialize this counter variable here so it’s going to be counter equal to 1 and also remember to add the semicolon here next we need to write the condition or the expression which will be evaluated to check whether you know we have to execute the statements inside this loop or not so here for the demonstration purpose we just want to print some text let’s say 30 times so here
we’re going to write counter less than or equal to 30 just because we want to execute the statements inside this loop till this counter variable is going to contain a value of 30 and when it becomes 31 at that time this expression is going to fail and you know the control will come out of this loop and next add a semicolon and next we need to write something called updating so as I told you in the while loop tutorial if we don’t update our counter variable or this loop counter variable then our program will be an infinite loop that’s why.
we need to make sure that at some point you know this condition or this expression will evaluate to false so we can all right counter plus so this initialization condition and the updation and him inside this loop.
we can write the statement that we want to execute so here it’s just going to be a CF statement and we can output the counter variables value also counter and arrow then learning lead rocks and and n line all right now I’m gonna save it and build building run this so you guys can see where our program has printed learning like rocks 1 to 30 times so what exactly happened here is you know when the system executes this for loop when this for loop is encountered for the first time the variable this counter variable will be initialized to the value specified here and then that condition is checked and if this condition is satisfied then the statements inside the loop is executed and then after executing all the statements inside this loop this counter variable will be updated and again this condition is checked and if it is satisfied all the statements will be executed this updating is done again condition is checked when this condition becomes false you know the control will come out of this for loop and another thing is you know.
if you guys want to create this variable here inside this for loop you guys can do that while initializing so I’m going to remove this here and I’m going to write int counter no problem but if you create this variable here then this variable counter is available only inside this for loop so you cannot refer this variable counter also just follows just remember that and also here in updating you guys don’t need to you know increment it every time it may be decrement o or if you guys want to add any number to this value you guys can do that but it must be updated process which is going to cause this expression to fail at some point all right guys now I’m going to say this build and run this so you guys can see you know our program has executed as it was doing before and the last thing that I want to guys to teach in this tutorial is let’s say.
if you guys want to create an infinite loop at that time what you guys can do is you guys can remove all these statements and leave the initialization blank to add a semicolon leave the condition blank then add a semicolon that’s it if I run this program this is going to be an infinite loop so I’m going to delete the counter variable here.