Tutorial by: Date: 2016-04-27 04:56:37
he for loop in C language is also used to iterate the statement or a part of the program several times, like while and do-while loop.
But, we can initialize and increment or decrement the variable also at the time of checking the condition in for loop.
Unlike do while loop, the condition or expression in for loop is given before the statement, so it may execute the statement 0 or more times.
For loop is better if number of iteration is known by the programmer.
The syntax of for loop in c language is given below:
Let's see the simple program of for loop that prints table of 1.
1 2 3 4 5 6 7 8 9 10
Enter a number: 2 2 4 6 8 10 12 14 16 18 20
Enter a number: 1000 1000 2000 3000 4000 5000 6000 7000 8000 9000 10000
If you don't initialize any variable, check condition and increment or decrement variable in for loop, it is known as infinitive for loop.
In other words, if you place 2 semicolons in for loop, it is known as infinitive for loop.
If you run this program, you will see above statement infinite times.