For Loop in C Programming

The For loop in C Programming is used to repeat a block of statements in a given number of times until the given condition is False. C For loop is one of the most used loops in any programming language. Let us see the syntax of the for loop in C Programming:

For loop in C Syntax

The syntax of the For Loop in C Programming is as follows:

for (initialization; test condition; increment/decrement operator)
 {
       //Statement 1
       //Statement 2
       ………
       //Statement n
 }

If you observe the above syntax, The for loop in c has three expressions separated by the semi-colons (;) and the execution of these expressions are as follows:

  • Initialization: It starts with the initialization statement so, initialization of counters variables is done first (Example, counter = 1 or i = 1.). The initialization section is executed only once at the beginning.
  • Test Condition: The value of the counter variable tested against the test condition. If the condition evaluated to True, it will execute the statements inside the For loop in C. If the condition fails, the compiler will terminate.
  • Increment & decrement operator: This expression executed after the end of each iteration. This operator helps to increase or decrease the counter variable as per our requirement.

Flow Chart for For loop in C

Below screenshot will show you the flow chart of the For Loop in C Programming language

For Loop in C Programming FLOW CHART

The execution process of the for loop in c is:

  1. Initialization: We initialize the counter variable(s) here. Example, i=1.
  2. Test condition: It will check the condition against the counter variable. If the condition is True, then it will execute the statements inside it. If the condition evaluated to False, then C Programming will exit from it
  3. After completing the iteration, it will execute the Increment and Decrement Operator inside the C for loop to increment or decrement the value.
  4. Again it will check the condition after the value incremented. As long as the condition is True, the statements inside it will execute.

For Loop in C Programming Example

The for loop C program allows the user to enter any integer values. Then it will calculate the sum of natural numbers up to the user entered number.

  1. Within this C for loop example, User can enter any value above 1 (In this example we are entering 20 ), and total variable is initialized to 0.
  2. Next, we assign the user-entered value to a number variable. Then we are going to check whether the counter variable (i) is less than or equal to the number.
  3. If the condition results true, i added to the total. Otherwise, it will exit from it. At the starting of the loop i =1 and n =20, so the condition will be True until i value is incremented to 21.
  4. In the next section, we used ++ operator to increment the i value. After the incrementing process, it will repeat the process until the condition results False (until I =21).
#include <stdio.h>

int main()
{
  int i, number, total= 0;

  printf("\n Please Enter any integer\n");
  scanf("%d", &number);
  
  for(i=1; i<= number; i++)
   {
     total = total + i;
   }

  printf("\nSum of n natural numbers is: %d\n", total); 
  return 0;
}

C program Output

 Please Enter any integer
20

Sum of n natural numbers is: 210

For Loop in C features

The For loop in C has the flexibility to omit one or more sections from the declaration. Although we can skip one or more sections from it, we have to put the semicolon (;) in place; otherwise, it will throw compilation error. The following examples show you the features of For loop in C programming

Initialize the counter variable can skip, as shown below:

int i=1;

for( ;i<=10;i++)

Here, the counter variable declared before it.

Like initialization, we can also skip the increment part.

int i=1;

for( ;i<=20; )
 {
   //statements     
   i++;
 }

The incrementing part declared in the body.

C For loop allows us to initialize more than one counter variable at a time with comma separate:

for(i=1,j=20;i<j; i++)

It also allows using multiple conditions. Instead of using comma we have to use the logical operator to separate the two condition

for(i=1,j=20;i<=10 && j>=20; i++)
 {
   //statements
   j++;
 }

Like the test condition, it allows us to use more than one increment operator as follows

for(i=1,j=1; i<=10&& j<=10; i++, j++)

NOTE: Initialization, increment and decrement operator section, we can use comma to separate multiple declarations. To separate the test conditions, you must use a logical operator to join conditions.

Nested For Loop in C Programming

In the example we will show you, How to nest one inside another, also called as nested for loop in C programming.

#include <stdio.h>

int main()
{
 int i, j;

 for (i=9; i<=10; i++)
  {
   for (j=1; j<=10; j++)
    {
      printf("%d * %d = %d\n",i ,j, i*j);
    }
  }
 return 0;
}
9 * 1 = 9
9 * 2 = 18
9 * 3 = 27
9 * 4 = 36
9 * 5 = 45
9 * 6 = 54
9 * 7 = 63
9 * 8 = 72
9 * 9 = 81
9 * 10 = 90
10 * 1 = 10
10 * 2 = 20
10 * 3 = 30
10 * 4 = 40
10 * 5 = 50
10 * 6 = 60
10 * 7 = 70
10 * 8 = 80
10 * 9 = 90
10 * 10 = 100

This for loop in c program prints multiplication table of 9 and 10.

In the first for loop, i initialized to value 9, and then it will check whether i is less than or equal to 10. This condition evaluated to True until i reaches to 11. If this expression result is True, then it will enter into second for loop. Otherwise, it will exit from it.

Iteration 1: for (i = 9; i <= 10; i++)
The condition (9 <= 10) is True. So it will enter into the second.

Nested Loop first iteration: (j = 1; j <= 10; j++)
The expression (1 <=10) is True. So statement inside it will printed
9 * 1 = 9
Next, the value of j will increment to 2

Nested Second iteration: (j = 2; 2 <= 10; 2++)
The expression (2 <=10) is True
9 * 2 = 9

It will repeat the process up to 10. Next j value is 11, expression (11 <= 10) fails. So compiler exits from the nested or inner for loop. It will only exit from the inner (Second) but not from the entire loop.

Iteration 2: (i = 10; 10 <= 10; 10++)
The expression (10 <= 10) is True. So it will enter into the second one.

Repeat the Nested for loop iteration.

Iteration 3: (i = 11; i <= 10; i++)
i = 11, and the condition evaluated to False, so it is terminated, No need to check the second one