Summary: in this tutorial, you will learn about C for loop statement to execute a block of code repeatedly.
Introduction to C for loop statement
The C for loop statement is used to execute a block of code repeatedly. It is often used when the number of iterations is predetermined. If the number of iterations is not predetermined, we often use the while loop or do while loop statement.
The following illustrates the syntax of the for loop statement:
1 2 3 | for (initialization_expression;loop_condition;increment_expression){ // execute statements } |
There are three expressions separated by the semicolons ( ;
) in the control block of the C for loop statement.
- The
initialization_expression
expression executes when the loop first starts. It is typically used to initialize a loop counter variable. - The
loop_condition
expression is evaluated at the beginning of each iteration. The execution of the loop continues until theloop_condition
evaluates tofalse
. - The
increment_expression
expression is evaluated at the end of each iteration. It is used to increase or decrease the loop counter variable.
The following flowchart illustrates the C for loop statement:

C for loop Flowchart
The following example demonstrates how to use C for loop statement to display numbers from 0 to 4:
1 2 3 4 5 6 7 8 9 10 | #include <stdio.h> int main() { const int max = 5; int i; for(i = 0; i < max; i++) printf("%d ",i); return 0; } |
And the output is:
1 | 0 1 2 3 4 |
Variations of the C for loop statement
C for loop is very flexible based on the combination of the three expressions. You can increase or decrease the loop counter. You can count by not only one but also two, three and so on. You can count by a character. Let’s take a look at the following program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | /* * File: main.c * Author: zentut.com * Purpose: Demonstrates C for loop : counter */ #include <stdio.h> #include <stdlib.h> int main() { /* count down from 10*/ int waits = 10; int s; for(s = waits; s > 0; s--) { printf("%d\n",s); } printf("Happy New Year!\n"); /* count by 3*/ for(s = 0; s < 10; s+=3) { printf("%d\n",s); } /*count by character */ char c; for(c = 'a'; c <= 'z'; c++) printf("ASCII(%c) = %d\n",c,c); return 0; } |
Because all expressions in the for loop statement are optional, you can omit any expression or all of them. When you omit all expressions, the for loop behaves slightly similar to the while loop or do while loop.
The following program demonstrates the flexibility of C for loop:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | /* * File: main.c * Author: zentut.com * Purpose: Demonstrates C for loop : omit expressions */ #include <stdio.h> #include <stdlib.h> int main() { int max = 5; /* omit the first and the last expressions*/ int c = 0; for(; c < max;) { c++; printf("c = %d\n",c); } /* omit all expressions*/ int i = 0; for(;;) { i++; /* use break to escape the loop*/ if(i > max) break; printf("i = %d\n",i); } return 0; } |
Nested for loop in C
You can put a for loop inside another for loop, which is called a nested for loop. The following example demonstrates how to use a nested for loop to display a pyramid of stars based on the number of levels.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | /* * File: main.c * Author: zentut.cm * Purpose: Demonstrates nested C for loop */ #include <stdio.h> #include <stdlib.h> int main() { int i, k, levels, space; printf("Enter the number of levels in pyramid:"); scanf("%d",&levels); space = levels; for ( i = 1 ; i <= levels ; i++ ) { for ( k = 1 ; k < space ; k++ ) printf(" "); space--; for ( k = 1 ; k <= 2*i - 1 ; k++ ) printf("*"); printf("\n"); } return 0; } |
For example, if you want to display a pyramid with 10 levels, you can see the following output:
1 2 3 4 5 6 7 8 9 10 11 | Enter the number of levels in pyramid:10 * *** ***** ******* ********* *********** ************* *************** ***************** ******************* |
Another example of using for loop statement to develop a program for displaying Pascal’s triangle. Simply put, in Pascal’s triangle, each number is the sum of the two numbers directly above it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | #include <stdio.h> typedef unsigned long long ULL; ULL factorial(const int); void pascal_triangle(const int); int main() { int rows; printf("--- C Pascal's Triangle Demo ---\n"); printf("Enter the number of rows for the Pascal's triangle:"); scanf("%d",&rows); pascal_triangle(rows); return 0; } /* calculate factorial of n */ ULL factorial(const int n) { int i; ULL f = 1; for(i = 1 ; i <= n ; i++ ) f = f * i; return f; } /* displays Pascal's triangle based on the number of rows (rows) */ void pascal_triangle(const int rows) { int i, j; ULL k; for ( i = 0 ; i < rows ; i++ ) { for(j = 0 ; j <= ( rows - i - 2 ) ; j++ ) printf(" "); for(j = 0 ; j <= i ; j++ ) { k = factorial(i)/(factorial(j)*factorial(i-j)); printf("%ld ",k); } printf("\n"); } } |
When you enter the number of rows for Pascal’s triangle: 8, you get the following output:
1 2 3 4 5 6 7 8 9 10 | --- C Pascal's Triangle Demo --- Enter the number of rows for the Pascal's triangle:8 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 1 6 15 20 15 6 1 1 7 21 35 35 21 7 1 |
In this tutorial, you have learned how to use various forms of the C for loop statement to execute a block of code repeatedly when the number of iterations is predetermined.