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
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.
In order to summarize program flow of a For loop in C programming, here are the steps:
- Condition Validation
- Execution of block statements
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.