Home >>C Tutorial >C for loop

for loop in C

For loop in C

The for loop in the C language is basically used to iterate the statements or the part of a program repeated times. The for loop is most frequently used to traverse the data structures in C such as the array and linked list.

Here is the syntax of the for loop in C language:

for(Expression 1; Expression 2; Expression 3){  
//code that is to be executed  
}  

Here is the flowchart of the for loop in C language:

Let's understand the concept of for loop with these examples 1. Here is an example depicting the printing the table of 1:

#include<stdio.h>  
int main()
{  
int i=0;        
for(i=1;i<=10;i++){      
printf("%d \n",i);      
}     
return 0;  
}     
Output :
1 2 3 4 5 6 7 8 9 10

2. Here is another example depicting the printing of the table of any number entered by the end user:

#include<stdio.h>  
int main()
{  
int i=1,number=0;      
printf("Enter a number of your choice: ");    
scanf("%d",&number);    
for(i=1;i<=10;i++)
{      
printf("%d \n",(number*i));    
}    
return 0;  
}
Output :

Enter a number of your choice: 4

4 8 12 16 20 24 28 32 36 40

Let's have a look at the properties of Expression 1 of for loop

  • The initialization of the loop variable is represented by the expression.
  • More than one variable can be initialized in Expression 1.
  • Expression 1of for loop is optional.
  • In C language the variables cannot be declared in Expression 1. But, It can occur as an exception in some compilers.

Here is an example of the expression 1:

#include <stdio.h>  
int main()  
{  
    int a,b,c;  
    for(a=0,b=12,c=23;a<2;a++)  
    {  
        printf("%d ",a+b+c);  
    }  
}  
Output :
35 36

Here is another example of the Expression 1 for a better understanding of the topic:

#include <stdio.h>  
int main()  
{  
    int i=1;  
    for(;i<5;i++)  
    {  
        printf("%d ",i);  
    }  
}  
Output :
1 2 3 4

Let's have a look at the properties of Expression 2 of for loop:

  • It is a conditional expression that checks for a specific condition to be satisfied. If the condition is not satisfied then the loop is terminated.
  • More than one condition is allowed in Expression 2. Until the last condition becomes false, the loop will continue iterating and the other conditions are treated as statements.
  • Expression 2 is known as optional.
  • The task of expression 1 and expression 3 can be performed by Expression 2. This simply means, the variable can be initialized by the user as well as update the loop variable in expression 2 by itself.
  • Zero or non-zero value in expression 2 can be passed by the user. Any non-zero value is true and zero is false by default in the C language.

Here is an example of Expression 2:

#include <stdio.h>  
int main()  
{  
    int i;  
    for(i=0;i<=4;i++)  
    {  
        printf("%d ",i);  
    }  
}  
Output :
0 1 2 3 4

Here is another example of Expression 2:

#include <stdio.h>  
int main()  
{  
    int i,j,k;  
    for(i=0,j=0,k=0;i<4,k<8,j<10;i++)  
    {  
        printf("%d %d %d\n",i,j,k);  
        j+=2;  
        k+=3;  
    }  
}  
Output :
0 0 0
1 2 3
2 4 6
3 6 9
4 8 12

Here is another example of Expression 2:

#include <stdio.h>  
int main()  
{  
    int i;  
    for(i=0;;i++)  
    {  
        printf("%d",i);  
    }  
}    
Output :
infinite loop

Let's have a look at the properties of Expression 3 of for loop:

  • In order to update the loop variable, expression 3 is used.
  • More than one variable can be updated at the same time.
  • Expression 3 is known to be optional in C language.

Here is an example of Expression 3:

#include<stdio.h>  
void main ()  
{  
    int i=0,j=2;  
    for(i = 0;i<5;i++,j=j+2)  
    {  
        printf("%d %d\n",i,j);  
    }  
}  
Output :
0 2
1 4
2 6
3 8
4 10

Let's understand the loop body in brief

The braces {} are used in order to define the scope of the loop {} braces are used. There is no need to use braces if the loop constitutes of only one statement. A loop can be possible without a body. The braces basically act as a block separator, i.e., the value of the declared variable inside for loop is only valid for that particular block.

Here is an example for the better understanding of the loop body:

#include<stdio.h>  
void main ()  
{  
    int i;  
    for(i=0;i<10;i++)  
    {  
        int i = 20;  
        printf("%d ",i);  
    }  
}  
Output :
20 20 20 20 20 20 20 20 20 20

Infinitive for loop in the C Language

There is no need to supply any expression in the syntax to produce an infinite for loop in the C language. As an alternative, the user need to provide two semicolons in order to validate the syntax of the for loop in C language. Then the result will act as an infinite for loop.

#include<stdio.h>  
void main ()  
{  
    for(;;)  
    {  
        printf("welcome to Phptpoint");  
    }  
}  
Output :
The above mentioned statement in the example will be displayed for an infinite times.

No Sidebar ads