C - For Loop

C Programming

Share

In this tutorial we will learn about for loop in C programming language.

For loop is similar to the while loop and the do-while loop. The only difference is that the for loop is more compact as it has the initialisation, condition and update part at the same place.

Syntax of a for loop is given below.

for (initialise; condition; update) {
  //code...
}

We use the for keyword to create a for loop. The for loop has four parts namely the initialise, condition, update and the body.

The initialise part of the for loop is executed first and is executed only once.

After initialise part the condition part is checked. If the condition is satisfied then the body of the for loop is executed.

After the execution of the body the update part is executed.

Then we recheck the condition after update. If the condition still satisfies then the body is re-executed otherwise, we exit the loop.

In the following example we will print 1 to 10 using the for loop.

#include <stdio.h>
int main(void)
{
  //variable
  int i;
  
  //loop
  for (i = 1; i <= 10; i++) {
    printf("%d\n", i);
  }
  
  printf("End of code\n");
  return 0;
}

Output

1
2
3
4
5
6
7
8
9
10
End of code

In the above code we are using a variable i and we are initialising it to 1 in the initialise part of the for loop.

After setting i = 1 we are checking the condition i <= 10 i.e., whether i is less than or equal to 10. In this case it is true so, we enter the body of the for loop and print the value of i using the printf statement.

Next we move to the update part of the for loop where we increase the value of i by 1 using the increment operator.

Then we check the condition i <= 10. If it is still true then we execute the body of the loop othewise we exit the loop.

Nested for loops

We can nest a for loop inside another for loop thus creating a nested loop.

Syntax of a nested for loop inside another for loop.

//1st for loop
for (initialise_1; condition_1; update_1) {
  //2nd for loop
  for (initialise_2; condition_2; update_2) {
    //code...
  }
}

In the above nested for loops the initialise_1 is first executed then the condition_1 is checked. If the condition is satisfied then we enter the body of the first loop.

Inside the body of the for loop 1, we first execute initialise_2 then we check the condition condition_2 and if the condition is satisfied we execute the body of the second for loop.

After executing the second for loop we run the update_2 and then recheck condition_2. If the condition is satisfied we rerun for-loop-2 otherwise, we exit the second for loop.

Following flowchart will explain how the nested for loops are working.



In the following example we will print the given pattern using nested for loops.

Pattern
1 2 3
4 5 6
7 8 9

Code

#include <stdio.h>
int main(void)
{
  //variable
  int r, c, count;
  
  //loop
  for (r = 1, count = 1; r <= 3; r++) {
    for (c = 1; c <= 3; c++, count++) {
      printf("%d ", count);  //this will print the digit
    }
    printf("\n");  //this will take us to new line
  }
  
  printf("End of code\n");
  return 0;
}

Output

1 2 3 
4 5 6 
7 8 9 
End of code

In the above code we have 3 variables r, c and count where, r denotes the row, c denotes the column and count is the count of numbers from 1 to 9.

Looking at the given pattern we can tell that there are 3 rows and 3 columns and we are counting from 1 to 9. So, r will start from 1 and end at 3. Similarly, c will start from 1 and end at 3. And count will start from 1 and end at 9.

So, in the initialise_1 part we are setting r = 1 and count = 1.

Then we are checking the condition_1 i.e., r <= 3 which means we will execute the body of the 1st for loop as long as r is less than or equal to 3 i.e., 3 times.

Then we enter the body of the 1st for loop. Inside it we have the 2nd for loop.

So, in the initalise_2 part we are setting c = 1 and then checking the condition_2 i.e., c <= 3 i.e., whether c is less than or equal to 3. This means we will run the 2nd for loop 3 times.

Inside the body of the 2nd for loop we are printing the value of count.

Then we are executing update_2 i.e., increasing the value of c by 1 and count by 1.

When condition_2 is not satisfied we exit the 2nd for loop and execute the update_1 i.e., we increase the value of r by 1 and recheck condition_1.

We exit the 1st for loop only when condition_1 is also not satisfied.

Share