The syntax for a for loop :
for (initialise; condition; update) { //code... }
We create a for loop using the 'for' keyword.
The for loop contains four parts, initialise, condition, update, and body.
The initialization part is executed first and only once.
After initializing the part, check the condition part.
If the condition is true, the body of the for loop is executed.
After executing the body, the update section is executed.
Then we re-check the conditions after the update.
If the condition is true, we re-execute the body, otherwise, we exit the loop.
In the example below, we will print 1 through 10 using a for loop.
#include <stdio.h> int main(void) { //variable int i; //loop for (i = 1; i <= 10; i++) { printf("%d\n", i); } printf("End of code\n"); return 0; }
1 2 3 4 5 6 7 8 9 10 End of code