Understanding the For Loop in C: Structure and Usage

The for loop is a fundamental construct in C programming, enabling efficient iteration through code blocks. Unlike other loop types such as while and do-while, the for loop is an entry-controlled loop that allows initialization, test condition, and increment to be specified in one concise line. This makes it the preferred choice for many programmers when executing statements based on a specific condition.



For Loop in C

Most programming languages, including C, support the for keyword for constructing loops. In C, the other loop-related keywords are while and do-while. Unlike the other two types, the for loop is called an automatic loop and is usually the first choice of programmers.

The for loop is an entry-controlled loop that executes the statements till the given condition. All the elements (initialization, test condition, and increment) are placed together to form a for loop inside the parenthesis with the for keyword.

Syntax of for Loop

Syntax

for (init; condition; increment){
   statement(s);
}

Control Flow of a For Loop

Here is how the control flows in a for loop:

  1. The init step is executed first, and only once. This step allows you to declare and initialize any loop control variables.
  2. Next, the condition is evaluated. If it is true, the body of the loop is executed. If it is false, the loop ends, and control jumps to the next statement after the for loop.
  3. After the body of the for loop executes, the control jumps back up to the increment statement. This updates the loop control variables.
  4. The condition is evaluated again. If true, the loop repeats. If false, the loop ends.

Developers prefer to use for loops when they know in advance how many iterations are needed. The for loop can be employed with different variations. Let's look at some examples:

Example: Basic for Loop

Example

#include 

int main(){
   int a;

   // for loop execution
   for(a = 1; a <= 5; a++){
      printf("a: %d\n", a);
   }

   return 0;
}
Output

a: 1
a: 2
a: 3
a: 4
a: 5

Example: Initializing Loop Counter Before Loop Statement

Example

#include 

int main(){
   int a = 1;

   // for loop execution
   for( ; a <= 5; a++){
      printf("a: %d\n", a);
   }
   return 0;
}
Output

a: 1
a: 2
a: 3
a: 4
a: 5

Example: Updating Loop Counter Inside for Loop Body

Example

#include 

int main(){
   int a;

   // for loop execution
   for(a = 1; a <= 5; ){
      printf("a: %d\n", a);
      a++;
   }
   return 0;
}
Output

a: 1
a: 2
a: 3
a: 4
a: 5

Example: Using Test Condition Inside for Loop Body

Example

#include 

int main(){
   int a;

   // for loop execution
   for(a = 1; ; a++){
      printf("a: %d\n", a);
      if(a == 5)
      break;
   }
   return 0;
}
Output

a: 1
a: 2
a: 3
a: 4
a: 5

Example: Using for Loops with Multiple Counters

Example

#include 

int main(){
   int a, b;

   // for loop execution
   for(a = 1, b = 1; a <= 5; a++, b++){
      printf("a: %d b: %d a*b: %d\n", a, b, a*b);
   }

   return 0;
}
Output

a: 1 b: 1 a*b: 1
a: 2 b: 2 a*b: 4
a: 3 b: 3 a*b: 9
a: 4 b: 4 a*b: 16
a: 5 b: 5 a*b: 25

Example: Decrement in for Loop

Example

#include 

int main(){
   int a;

   // for loop execution
   for(a = 5; a >= 1; a--){
      printf("a: %d\n", a);
   }

   return 0;
}
Output

a: 5
a: 4
a: 3
a: 2
a: 1

Example: Traversing Arrays with for Loops

Example

#include 

int main(){
   int i;
   int arr[] = {10, 20, 30, 40, 50};

   // for loop execution
   for(i = 0; i < 5; i++){
      printf("arr[%d]: %d\n", i, arr[i]);
   }

   return 0;
}
Output

arr[0]: 10
arr[1]: 20
arr[2]: 30
arr[3]: 40
arr[4]: 50

Example: Sum of Array Elements Using for Loop

Example

#include 

int main(){
   int i;
   int arr[] = {10, 20, 30, 40, 50};
   int sum = 0;
   float avg;

   // for loop execution
   for(i = 0; i < 5; i++){
      sum += arr[i];
   }
   avg = (float)sum / 5;
   printf ("Average = %f", avg);

   return 0;
}
Output

Average = 30.000000

Example: Factorial Using for Loop

Example

#include 

int main(){

   int i, x = 5;
   int fact = 1;

   // for loop execution
   for(i = 1; i <= x; i++){
      fact *= i;
   }
   printf("%d! = %d", x, fact);

   return 0;
}
Output

5! = 120

The for loop is ideally suited when the number of repetitions is known. However, the looping behavior can be controlled by the break and continue keywords inside the body of the for loop. Nested for loops are also commonly used in processing two-dimensional arrays.