Learn one of the most powerful programming languages in the world and become a rockstar developer.

FOR LOOP Statement

in this tutorial, you will learn about C for loop statement to execute a block of code repeatedly.

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:

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 the loop_condition evaluates to false.
  • The increment_expression expression is evaluated at the end of each iteration. It is used to increase or decrease the loop counter variable.
# Print numbers from 1 to 10
#include <stdio.h>
int main() {
  int i;
  for (i = 1; i < 11; ++i)
    printf("%d ", i);
  return 0;
Output :
1 2 3 4 5 6 7 8 9 10
Explanation :
  1. At first, i is initialized to 1.
  2. The test expression i < 11 is evaluated. Since 1 less than 11 is true, the body of forloop is executed. This will print the 1 (value of i) on the screen.
  3. The update statement ++i is executed. Now, the value of i will be 2. Again, the test expression is evaluated to true, and the body of for loop is executed. This will print 2(value of i) on the screen.
  4. Again, the update statement ++i is executed and the test expression i < 11 is evaluated. This process goes on until i becomes 11.
  5. When i becomes 11, i < 11 will be false, and the for loop terminates.
// Program to calculate the sum of first n natural numbers
// Positive integers 1,2,3...n are known as natural numbers
#include <stdio.h>
int main()
    int num, count, sum = 0;
    printf("Enter a positive integer: ");
    scanf("%d", &num);
    // for loop terminates when num is less than count
    for(count = 1; count <= num; ++count)
        sum += count;
    printf("Sum = %d", sum);
    return 0;
Output :

Enter a positive integer: 10
Sum = 55

Previous Next
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Privacy Policy That's Fine