C for loop
The for loop is another entry-controlled loop and very similar to while loop. It is a very concise loop where everything is defined in a single line. The general form is:
Syntax
for(initialization; test_condition; increment) {
body of the loop
}
Here in the for loop initialization of the control variables is done first. Then the value of the control variable is tested using the test_condition. If the test_condition is true then the body of the loop will be executed otherwise the loop will be terminated and continues with the statement immediately after the body of the loop.
When the body of the loop is executed the control is transferred back to the for statement after evaluating the last statement in the loop. Then the control variable is incremented using an increment operator. The new value again tested for the condition. As long as the condition is true the process continues.
Flowchart of for loop:
Example of a for loop:
Example:
#include <stdio.h>
int main()
{
for(i = 1; i<=10; i++) {
printf("%d\n", i);
}
return 0;
}
Output:
1 2 3 4 5 6 7 8 9 10