for loop in C


Reading time: 15 minutes | Coding time: 2 minutes

Loop is a programming concept which is natively supported by nearly all programming languages. Loops are used to repeat a particular coding task where each repetition follows a particular pattern which can be incorporated in a code. Example of such a task is printing integers from 1 to 10. The repetition is that we are printing integers and the pattern is that we start from 1 and increment by 1 each step. In this article, we will take a look at loops in C.

The concept of loops comes into picture when we need to implement/print a particular block/statement more than one time till a particluar condition is specified. We have different types of loops :

  1. Entry Controlled loop : Where the entry of the loop is controlled that is we have a condition at the starting of the loop. Example : while loop

  2. Exit Controlled loop : In this type of loop the exit from the loop is controlled that is we have a condition at the end of the loop. Example : do..while loop

topic of image

In this particular section we are going to discuss for loop

Syntax :

for (initializationStatement; testExpression; updateStatement)
{
           // codes 
}

Where :

  • initializationStatement is the statement where we to initialize the loop counter to some value. Example : int i=0;

  • testExpression in this expression we have to test the condition. If the condition is to true then we will execute the body of loop otherwise we will exit from the for loop. Example : i<=10;

  • updateStatement after executing loop body this statement is encountered and is incremented/decremented depending on the statement. Example : i++

Order of the excution of for loop

  1. First we intialize the loop counter in C one have to declare the variable before using it while in C++ it can be declared as well as intialized in the intializationStatement.

  2. Then the condition is checked i.e testExpression is evaluated.

  3. After this we enter the loop and print/execute the statement(s)/block.

  4. Then the value of the loop counter in incremented/decremented depending on the updateStatement.

  5. Now we will repeat Step 2-4 till the testExpression condition is violated.

topic of image

Exmaple

In this example, we will write a program to print number from 1 to 10.

Answer :

#include<stdio.h>
#include<conio.h>
void main()
{
               int i;
               clrscr(); // clear the o/p screen
               for(i=0;i<10;i++)
               {
                   printf("%d \n",i+1);
               }
               getch(); // hold the o/p screen
}

Difference between for and while loop

Basis for loop while loop
Declaration for (initializationStatement; testExpression; updateStatement) { // code } While(testExpression) { //code }
Condition If condition is not mentioned then the loop will iterate infinite times. If condition is not mentioned then it provides compilation error.
Iteration Statement The iteration statement is written on the top hence executes only after all statements in loops are executed. The statement can be mentioned anywhere in the loop.
Use It is used when we already know the number of iterations. It is used when number of iterations are not exactly known.

Points to remember

  1. It is not compulsory to specify initializationStatement or testExpression or updateStatement during declaration of for loop. -> You can declare updatStatement inside the block also.
    Example :
#include<stdio.h>
#include<conio.h>
void main()
{
        int i;
        clrscr(); // clear the o/p screen
      for(i=0;i<10; )
    {
        printf("%d \n",i+1);
        i++
    }
    getch(); // hold the o/p screen
}

Do try for initializationStatement and testExpression

  1. One can include more than one initializationStatement or testExpression or updateStatement during declaration of for loop this can be done with the help of logical operations such as AND(&&) / OR(||).

  2. You should always declare the loop with small case i.e for and not FOR/For/FoR...etc as C is a case sensitive langauage and this true for all other loops as well

Question

What is this for(;;;); applicable for?

Infinite loop
Invalid loop
0 loop
1 loop
This statement is applicable and is known as infinite loop i.e the loop will not have any terminating condition and will continue running till we exit the program.