|for||loops through a block of code a specified number of times.|
|while||loops through a block of code as long as the specified condition is true.|
|do while||loops through a block of code once, and then repeats the loop as long as the specified condition is true.|
for loop iterates the block of code for a fixed number of times. It is used when the number of iteration is known.
The syntax of for loop is given below.
The parameters of the for loop statement have following meanings:
Here in this example, firstly
'i' is initialized a value, then a condition is given to control the loop and then the
increment operator is used to increment the value of 'i'. In this loop every value of 'i' will get printed on the screen till the given condition is true.
Here in the example below, an array storing 5 values is iterated by the help of
for loop. The index value of the array is incremented with every loop and then one by one all the values of the array gets printed on the screen. Take a look in try-it editor to understand the working of
for loop in arrays.
Note: If the updated content is not visible to you please refresh the cookies by pressing CTRL + Shift + R.