English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C Language Basic Tutorial

C Language Flow Control

C Language Functions

C Language Arrays

C Language Pointers

C Language Strings

C Language Structure

C Language File

C Other

C Language Reference Manual

C Language for Loops

In this tutorial, you will learn how to create a for loop in C programming with the help of examples.

In programming, loops are used to repeat code blocks until a specified condition is met.

C programming has three types of loop structures:

  1. for Loop

  2. while Loop

  3. do... while Loop

We will learn about the for loop in this tutorial. In the next tutorial, we will learn about the while and do...while loops.

for Loop (Loop)

The syntax of the for loop is:

for (initializationStatement; testExpression; updateStatement)
{
    //The statements within the loop body
}

How does the for loop work?

  • The initialization statement (initializationStatement) is executed only once.

  • Then, evaluate the test expression (testExpression). If the result of the calculation of the test expression (testExpression) is false, the for loop will terminate.

  • But if the test expression (testExpression) is evaluated as true, then the statements within the for execution loop will be executed, and the update expression (updateStatement) will be updated.

  • Evaluate the test expression (testExpression) again.

This process continues until the test expression (testExpression) is false. When the test expression (testExpression) is false, the loop terminates.

To learn more about test expressions (when test expressions are evaluated as true and false), please seerelationoperatorsandlogical operators.

for loop flowchart

Example1: for loop

//print from1to10of the numbers
#include <stdio.h>
int main() {
  int i;
  for (i = 1; i < 11; ++i)
  {
    printf("%d ", i);
  }
  return 0;
}

Output result

1 2 3 4 5 6 7 8 9 10
  1. i is initialized to1.

  2. the test expression i < 11. Since1less than11is true, so the for loop executes the loop body. This will print1of (i's value).

  3. and execute the update statement++i. Now, the value of i becomes2. Again, the test expression is evaluated as true, and the for loop body is executed. This will print2of (i's value).

  4. Similarly, execute the update statement++i and evaluate the test expression i < 11. This process continues until i is11.

  5. when i becomes11when i < 11will be false, and the for loop will terminate.

Example2: for loop

//The program calculates the sum of the first n natural numbers
//positive integer1,2,3 ... n is called a natural number
#include <stdio.h>
int main()
{
    int num, count, sum = 0;
    printf("Enter a positive integer: ");
    scanf("%d", &num);
    // The for loop terminates when num is less than count
    for(count = 1; count <= num; ++count)
    {
        sum += count;
    }
    printf("Sum = %d", sum);
    return 0;
}

Output result

Enter a positive integer: 10
Sum = 55

The value entered by the user is stored in the num variable. Suppose the user entered10.

will initialize count to1and evaluate the test expression. Since the test expression count<=num (less than or equal to10of1is true, so the for loop will execute the loop body, and the value of sum will be equal to1.

Then, execute the update statement++count, the counter will be equal to2. Again, evaluate the test expression. Since2and less than10Therefore, the test expression is evaluated as true, and the for loop body is executed. Now, sum is equal to3.

Continue with this process, and calculate the sum until count reaches11.

When the value of count is11When, the evaluation result of the test expression is 0 (false), and the loop terminates.

Then, the value of sum is printed on the screen.

In the next tutorial, we will learn about while loops and do...while loops.