For Loop

The for loop in C language is used to iterate the statements or a part of the program several times. It is frequently used to traverse the data structures like the array and linked list.

Syntax

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


    
        //Let's see the simple program of for loop that prints table of 1.
        
        #include<stdio.h>

        int main(){
            int i=0;
            
            for(i=1;i<=10;i++){
                printf("%d \n",i);
            }

            return 0;
        }
    


    
        // C Program: Print table for the given number using C for loop
        
        #include<stdio.h>

        int main(){
            int i=1,number=0;

            printf("Enter a number: ");
            scanf("%d",&number);
            
            for(i=1;i<=10;i++){
                printf("%d \n",(number*i));
            }

            return 0;
        }
    


Properties of Expression 1

The expression represents the initialization of the loop variable.
We can initialize more than one variable in Expression 1.
Expression 1 is optional.

    
        #include<stdio.h>

        int main()
        {
            int a,b,c;

            for(a=0,b=12,c=23;a<2;a++)
            {
                printf("%d ",a+b+c);
            }

        }
    


    
        #include<stdio.h>

        int main()
        {
            int i=1;

            for(i=0;i<5;i++)
            {
                printf("%d ",i);
            }

        }
    


Properties of Expression 2

Expression 2 is a conditional expression. It checks for a specific condition to be satisfied.
If it is not, the loop is terminated.
Expression 2 can have more than one condition. However, the loop will iterate until the last condition becomes false. Other conditions will be treated as statements.
Expression 2 is optional.
Expression 2 can perform the task of expression 1 and expression 3. That is, we can initialize the variable as well as update the loop variable in expression 2 itself.
We can pass zero or non-zero value in expression 2. However, in C, any non-zero value is true, and zero is false by default.

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


    
        #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;
            }
        }
    


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


Properties of Expression 3

Expression 3 is used to update the loop variable.
We can update more than one variable at the same time.
Expression 3 is optional.

    
        #include<stdio.h>

        int main ()
        {
            int i=0,j=2;
            for(i = 0;i<5;i++,j=j+2)
            {
              printf("%d %d\n",i,j);
            }
        }
    



Loop body

The braces {} are used to define the scope of the loop. However, if the loop contains only one statement, then we don't need to use braces. A loop without a body is possible.

The braces work as a block separator, i.e., the value variable declared inside for loop is valid only for that block and not outside.

    
        #include<stdio.h>

        int main ()
        {
            int i;
            for(i=0;i<10;i++)
            {
                int i = 20;
                printf("%d ",i);
            }
        }
    



Infinitive for loop

To make a for loop infinite, we need not give any expression in the syntax. Instead of that, we need to provide two semicolons to validate the

syntax of the for loop. This will work as an infinite for loop.

    
        #include<stdio.h>

        int main ()
        {
            for(;;)
            {
                printf("welcome to My Website");
            }
        }