for Loop


Loop

In programming, a loop enables the execution of a block of commands over and over again until a specific condition is met. You will get to know detail about loop with examples.

Advantages of Loop

  • No need to write code again and again
  • This reduces errors
  • It provides code reusability
  • Improve readability
  • Traverse over element of data structures

C programming has three types of loops:

  1. for loop
  2. while loop
  3. doโ€ฆwhile loop

In this tutorial, you will learn about for loop and Nested for Loop with examples.


for Loop

A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times.

Syntax

The basic syntax for a for loop in C programming is as follows:

for (initialization; condition; update) {
    // code to be executed repeatedly
}

Hereโ€™s how it the for loop works with examples:

  • initialization: This expression executes only once at the beginning of the loop. It is typically used to declare and/or initialize a counter variable, but it can also be used for other purposes. For example, int i = 0 would be a common initialization statement.
  • condition: Before each iteration of the loop, the condition is checked. If the condition is true, the loop body is executed. If the condition is false, the loop stops. For example, i < 10 would be a common condition.
  • update: This expression runs after each loop iteration, usually to change the counter variable, but it can also serve other purposes. For example, i++ would be a common update statement.


For instance:

#include <stdio.h>

int main() {
  int i;
  for (i = 0; i < 5; i++) {
    printf("%d\n", i);
  }

  return 0;
}

Output

0 
1
2
3
4

Hereโ€™s how it the for loop works:

  1. Declaration and initialization: We declare and set the variable โ€œiโ€ to 0, and we will use it as a counter for the loop.
  2. Loop condition: The loop will continue executing as long as the condition โ€œi < 5โ€ is true. This means the loop will iterate five times since โ€œiโ€ starts at 0 and increments by 1 in each iteration.
  3. Loop body: The block of code inside the curly braces following the โ€œforโ€ statement is the loop body. In this case, we use the printf() function to display the value of โ€œiโ€ and add a new line character afterwards.
  4. Iteration: In each loop iteration, we use printf() to print the value of โ€œiโ€. At the beginning, โ€œiโ€ is 0, so it gets printed in the first iteration. Then, we increment โ€œiโ€ by 1 using the expression โ€œi++โ€.
  5. Loop termination: When the value of โ€œiโ€ becomes 5, the condition โ€œi < 5โ€ becomes false, and the loop terminates. The program continues with the next line of code after the loop.

Nested Loopsย 

A nested loop is a loop inside another loop. This means that the inner loop runs several times for each iteration of the outer loop.

Syntax

The basic syntax of a nested for loop in C programming is as follows:

for (int i = 0; i < n; i++) {    // outer loop
    for (int j = 0; j < m; j++) {    // inner loop
        // code to be executed repeatedly
    }
}

In this, the outer loop executes n times, and for each iteration of the outer loop, the inner loop executes m times. This means the inner loop will run n times multiplied by m times, resulting in a total of n*m executions of the code inside.

For instance:

#include <stdio.h>

int main() {
  int i, j;
  
  // Outer loop
  for (i = 1; i <= 2; ++i) {
    printf("Outer: %d\n", i);  // Executes 2 times
    
    // Inner loop
    for (j = 1; j <= 3; ++j) {
      printf(" Inner: %d\n", j);  // Executes 6 times (2 * 3)
    }
  }

  return 0;
}

Output

Outer: 1
 Inner: 1
 Inner: 2
 Inner: 3
Outer: 2
 Inner: 1
 Inner: 2
 Inner: 3

Hereโ€™s how it works:

  1. Header inclusion: The code starts by including the <stdio.h> header file, which provides the necessary function prototypes for input and output operations, including printf().
  2. ย The main() function: The main() function is the entry point of the program and where the execution begins.
  3. Variable declaration: We declare two integer variables, i and j, to use as loop counters for the outer and inner loops, respectively.
  4. Outer loop: During every time the outer loop runs, a second loop runs inside it. It initializes i to 1 and continues as long as i is less than or equal to 2. In each iteration, the loop prints the value of i using printf() with the โ€œOuter: %dโ€ format specifier.
  5. Inner loop: Within each iteration of the outer loop, an inner loop is executed. The inner loop also uses a for loop construct. It initializes j to 1 and continues as long as j is less than or equal to 3. In each iteration of the inner loop, we print the value of j using printf() and the format specifier โ€œInner: %dโ€.
  6. Execution flow: The outer loop executes two times (when i is 1 and 2). Within each iteration of the outer loop, the inner loop executes six times (when j is 1, 2, and 3). As a result, the program prints the message โ€œOuter: xโ€ twice and the message โ€œInner: yโ€ six times for each iteration of the outer loop.
  7. Program termination: When the loops finish running, the program encounters the โ€œreturn 0โ€ statement, which shows that the program has successfully ended.

ย