Control flow diagram of for loop in C programming with initialization, condition check, loop body, and update steps shown visually.

Understanding For Loop Control Flow in C Programming

Let’s learn how the for loop works in the C programming language.
The diagram illustrates the step-by-step control flow of a for loop in C.

Control Flow of a For Loop in C:
The operation of a for loop in C follows this structured sequence:

1. Initialization (Declaration): This step executes only once, before the loop starts. In this section, the loop control variable is declared and initialized (e.g., int i = 0;). These variables have local scope within the loop.

2. Test Expression (Condition Check): The test condition is evaluated. If it returns true, the loop continues to the next step. If it returns false, the loop terminates, and control moves to the statement following the loop.

3. Loop Body Execution: If the test condition is satisfied, the statements inside the loop body are executed.

4. Update Expression: After the loop body executes, the control variable is updated (e.g., i++;). This step is crucial for progressing toward the loop termination condition.

5. Repeat: Control returns to step 2 (test expression). This cycle continues until the test condition evaluates to false.

Key Point: If the initialization, test condition, or update expression is omitted, the loop may become an infinite loop. For example:

for( ; ; ) {
  // Infinite loop body
}

This syntax creates an infinite for loop because no condition ever stops it.


C Program to Demonstrate the For Loop

/* This C program prints the numbers from 1 to 10 using a for loop */

#include <stdio.h>

int main() {
 int i;
 printf("\nFor loop to display numbers from 1 to 10\n");
 for(i = 1; i <= 10; i++) {
  printf(" %d", i);
 }
 return 0;
}

Output:
For loop to display numbers from 1 to 10 1 2 3 4 5 6 7 8 9 10

Explanation:
The variable i is declared as an integer and initialized to 1.
The program first prints the message: For loop to display numbers from 1 to 10.
Then the for loop begins:

- The loop starts by setting i = 1.
- It checks if i <= 10. Since 1 is less than or equal to 10, the loop body executes, printing the current value of i.
- After printing, i increments by 1 (i++).
- The condition is checked again with the updated value of i.
- This process repeats, printing numbers from 1 through 10.
- When i becomes 11, the condition i <= 10 fails, and the loop stops.

The program then ends, having displayed the numbers 1 to 10 on the screen.


C Program to Illustrate Use of for(;;) Loop

/* C program below displays a series of numbers from 1 to 10 using a for(;;) loop */

#include <stdio.h>

int main()
{
  int i = 1;
  for(;;)
  {
    printf(" %d", i);
    i++;
    if(i > 10)
      break;
  }
  return 0;
}

Output: 1 2 3 4 5 6 7 8 9 10

Explanation:

In the above C program, the for(;;) loop is used — which is a type of infinite loop.

We first declare and initialize i = 1.

Now let’s go step-by-step through the loop execution:

- The control enters the for(;;) loop. Since there is no initialization, condition, or update inside the loop header, the loop doesn't check any condition and directly enters the body.

- The statement printf(" %d", i); is executed, which prints the value of i — in the first iteration, it's 1.

- Then, i++; increments the value of i by 1. Now, i becomes 2.

- The if(i > 10) condition is checked. Since i = 2, the condition is false, so the break statement is skipped.

- Control goes back to the top of the loop. Again, since the loop header doesn’t have any test condition, it continues and enters the loop body again.

- This process repeats: i is printed, incremented, and then checked against the condition if(i > 10).

- When i becomes 11, the condition if(i > 10) becomes true. So, the break statement executes, which stops the loop.

- The control comes out of the loop, and since there are no more statements in the program, it ends.

So even though for(;;) is an infinite loop, we’ve controlled its execution using the if and break statements.


Previous Topic:-->> while do while loop Assignments || Next topic:-->> For Loop assignments.


Other Topics: