For Loop in C Programming

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

C For Loop 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 language has three expressions separated by the semi-colons (;), and the execution of these expressions is 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 is 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 is executed after the end of each iteration. This operator helps increase or decrease the counter variable per our requirement.

Flow Chart of For loop in C

The below screenshot will show you the C programming for loop flow chart.

For Loop in C Programming FLOW CHART

The execution process of the C for loop is:

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

For Loop in C Programming Example

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

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

C program Output

For Loop in C Programming 1

C For Loop Initialization features

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

Initialize the counter variable can skip, as shown below:

int i=1;

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

Here, the counter variable is declared before it.

Like initialization, we can also skip the increment part.

int i=1;

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

The incrementing part is 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 a 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++;
 }

The final approach

Like the test condition, C for loop allows us to use more than one increment operator.

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

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

Nested For Loop in C Programming

In the example, we will show you, How to nest one inside another, also called a nested for loop in this 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;
}
Nested For Loop in C Programming

This C for loop program prints multiplication tables of 9 and 10.

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

Analysis

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

Nested one first iteration: (j = 1; j <= 10; j++)
The expression (1 <=10) is True. So the statement inside it will be 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 one. 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 C Nested for loop iteration.

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