For Loop in C Programming

The For loop in C Programming is used to repeat a block of statements for 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:

// C For Loop Syntax
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: For loop starts with the initialization statement so, initialization of counters variables is done first (For example, counter = 1 or i = 1.). The initialization section is executed only once at the beginning of the for loop.
  • Test Condition: The value of the counter variable tested against the test condition. If the condition is True, it will execute the statements inside the For loop in C. If the condition fails, the For loop 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 is:

  1. Initialization: We initialize the counter variable(s) here. For example, i=1.
  2. Test condition: It will check for the condition against the counter variable. If the condition is True, then it will execute the statements inside the for loop. If the condition is False, then C Programming will exit from the loop
  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 for the condition after the value incremented. As long as the condition is True, the statements inside the for loop 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.

/* For Loop in C Programming Example */
#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;
}

Output

 Please Enter any integer
20

Sum of n natural numbers is: 210

ANALYSIS

  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 the for loop. 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).

For Loop 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 the for loop, 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 the for loop.

Like initialization, we can also skip the increment part.

int i=1;

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

The incrementing part declared in the for loop 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++)

For loop 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, for loop in C allows us to use more than one increment operator as follows

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

NOTE: For initialization, increment and decrement operator section, we can use comma to separate multiple declarations. To separate the test conditions in C for loop, 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 for loop inside another for loop, also called as nested for loop in C  programming.

/* Nested For Loop in C Programming example */

#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;
}

Output

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 program prints multiplication table for 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 is True until i reaches to 11. If this condition is True, then it will enter into second for loop. Otherwise, it will exit from the for loop.

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

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

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

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

C For Loop Iteration 2: for (i = 10; 10 <= 10; 10++)
The condition (10 <= 10) is True. So it will enter into the second loop.

Repeat the Nested For loop iteration.

For Loop Iteration 3: for (i = 11; i <= 10; i++)
i = 11, and the condition is False. For loop is terminated, No need to check the second loop