For loop is the most commonly used and popular loop amongst all three loops present in C. The reason behind its popularity lies in the simplicity. For loop is the easiest loop to begin programming with loops.
Syntax of for loop:
for ( initialization ; condition ; update )
{
//Body of loop
}
{
//Body of loop
}
Parts of the for loop
For loop generally contains four parts:- First is the initialization. Here we initialize the counter variables.
- Second is the condition. Condition part contains one or more boolean expressions that determines whether the loop will further repeat or not. If the condition satisfies (condition is true) then the statement/s under body of loop will be executed else the loop terminates.
- Third is the body of loop. Body of loop generally contains one or more statement/s that are executed if the loop condition is true.
- And last is the updation. Updation part generally contains expressions to update the loop counter. Updation part is sometimes also referred as Increment/Decrement part.
Note: All the four parts of a for loop are optional. We may or may not specify any of the loop part.
Control flow of for loop
Working mechanism of for loop |
Few more things to note about for loop:
- Initialization part runs only once.
- The condition, body of loop and updation part runs repeatedly until the loop terminates.
Flow chart of for loop
Flow chart of for loop |
Sample program to demonstrate for loop
Write a program in C to print Hello, World! 10 times.Program:
#include <stdio.h> int main() { int i; //Used as a counter variable. for( i=1 ; i<=10 ; i++) { printf("Hello, World!\n"); } return 0; }Output:
Hello, World!
Hello, World!
Hello, World!
Hello, World!
Hello, World!
Hello, World!
Hello, World!
Hello, World!
Hello, World!
Hello, World!
Hello, World!
Hello, World!
Hello, World!
Hello, World!
Hello, World!
Hello, World!
Hello, World!
Hello, World!
Hello, World!
Note: For updation you may use any expressions that increments the value of i by 1. Such as you are free to use: i = i + 1; or i+=1; or i++;
Also if the body of loop contains single statement then there is no need to use curly braces { } for the for loop. As the above loop can also written as.
But it is always recommended that use pair of curly braces { } with the for loop whether it contains single or multiple statements.
Also if the body of loop contains single statement then there is no need to use curly braces { } for the for loop. As the above loop can also written as.
for ( i=1 ; i<=10 ; i++) printf("Hello, World!\n");
But it is always recommended that use pair of curly braces { } with the for loop whether it contains single or multiple statements.
Infinite for loop
An infinite for loop is a loop without any termination condition. Infinite for loop runs infinite times without terminating from the loop. They are generally used in programming when the number of repetitions are unknown by the programmer, for example: The CPU works in an infinite loop with its task as FETCH-DECODE-PROCESS. And keeps running infinitely until the user shuts down the PC.Processor working in an infinite loop |
Various examples of infinite for loop
Loop structure | Desccription |
for ( i=1 ; i<=10 ; ) printf("Hello, World!\n"); |
Here the updation part of the counter variable i is missing. Hence the value of i will remain 1 forever and the loop runs infinite times printing Hello, World! |
for ( i=1 ; ; i++ ) printf("Hello, World!\n"); |
Here the condition part of the loop is missing. Due to which the loop is forced to run repeatedly infinite times printing Hello, World! As there isn’t any condition to terminate. |
for ( i=1 ; ; ) printf("Hello, World!\n"); |
Here the condition as well as updation part is missing from the loop. Hence it will also iterate infinite times printing Hello, World! |
for ( ; ; ) printf("Hello, World!\n"); |
Here all the three parts initialization, condition as well as updation part is missing from the loop. Hence it will also run infinite times printing Hello, World! |
Empty for loop
As I said earlier all the four parts of the for loop are optional. Which means we may skip any part of the for loop. An empty loop is a loop without body of loop. Which means it’s a loop that does nothing instead of iterating certain times.Various examples of empty for loop
Loop structure | Description |
for ( i=1 ; i<=10 ; i++ ) ; | Here the body of the loop is missing from the loop. The loop may run 10 times but does nothing. |
for ( i=1 ; i<=10 ; i++ ) { } |
|
for ( ; ; ) ; | An empty for loop that runs infinite times. |