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.

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 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 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 to increase or decrease the counter variable as per our requirement.

Flow Chart of For loop in C

The below screenshot will show you the flow chart.

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 the condition against the counter variable. If the condition is True, then it will execute the statements inside it. If the condition is evaluated to False, then C Programming will exit from it
  3. After completing the iteration, it will execute the Increment and Decrement Operator inside it to increment or decrement the value.
  4. Again it will check the condition after the value is incremented. As long as the condition is True, the statements inside it will execute.

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 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 are going to 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, so 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 the process until the condition results 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

 Please Enter any integer

Sum of n natural numbers is: 210

For Loop 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 have to 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; )

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++)

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 commas 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 a nested for loop in this 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;
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 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.

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 to False, so it is terminated. No need to check the second one