For Loop in C Programming | C Tutorial

By | September 3, 2017

Let us implement For loop in C programming and understand how a while loop works in C programming with its algorithm, flowchart, example, nested for loop and explanation.

A For loop is a control flow statement that helps to repeatedly iterate a set of code based on a given Boolean condition. The while loop can be thought of as a repeating if – else statement.

For Loop Flowchart

For loop in C programming with syntax, flowchart, algorithm, example, output and explanation

This looping construct follows an entry condition system wherein the condition is tested before the program control moves into the inner blocks of code within the while block.

For Loop Syntax

As soon as the program control encounters a For loop, the very first step is the initialization of the variable. It then validates the test-condition.

If the condition evaluates to true then the inner block statements are executed. If the condition evaluates to false, then the program control skips the For loop.

As soon as the last statement within the For loop is executed, the program control comes to the third parameter which is increment/decrement operator and executes it.

For Loop Algorithm

After the increment or decrement operation completes, the control moves to the initialization section, then test condition and again the inner block statements.

This iteration continues until the condition is false. Once it evaluates to false, the program control skips the re-iteration process and executes the statements after the For loop.

 

Note: The C program of For loop is compiled with GNU GCC compiler using CodeLite IDE on Microsoft Windows 10 operating system.

C Program To Print Numbers using For Loop

To find more implementations of While loop in C programming, you must check these simple C programs for practice.

Alternatively, you can even skip the initialization and increment / decrement operation. Here’s a sample.

 

Output

For loop Algorithm with flowchart, syntax, algorithm, explanation

In order to summarize program flow of a For loop in C programming, here are the steps:

  1. Initialization
  2. Condition Validation
  3. Execution of block statements
  4. Increment/Decrement

Nested For Loop

A nested for loop is useful in many scenarios such as pattern programming, multi-dimensional arrays and much more. Let us see an example of a nested for loop in C programming.

The first for loop is the outer for loop and the second for statement is known as the inner loop.

If you have any compilation errors or doubts about For loop in C programming, let us discuss in the comment section below. Find more about the for loop on Wikipedia.

Let's Discuss