Loops in C programming

Loops in C programming

Looping is the process of executing same program multiple times or in a sequence until certain condition becomes false. Looping can be used to repeat any specific statement multiple times.

Looping consists of condition in which statement is checked every interval for true and false , start of the sequence which is needed to start the sequence of loop , statement to which loop is applied and step which runs through out the whole loop until it matches with the false condition.

Loop are of following types :

  1. For loop
  2. do loop
  3. while loop
  4. do .. while loop
For Loop

In case of for loop the syntax is as follows :

for(counter; condition ; step)
{
       statements ....
}

For example if “Hello World ” needs to be printed 5 times then the code for the program will be as follows

#include<stdio.h>
int main(){
     int i; //here i is used as the counter in loop 
     for( i = 1; i <= 5 ; i++)
     {
           printf("Hello World \n");
     } 
     return 0;
}

The output of the above program will be :

Hello World
Hello World
Hello World
Hello World
Hello World

Now the flow of the program can be interpreted here as

Value of i Condition i <= 5i++Output
11<=5 i.e true
Hello World


i++ i.e. i=1+1= 2
22 <= 5 i.e true
Hello World


i=2+1=3
33 <= 5 i.e true
Hello World


i=3+1=4
44 <= i.e true
Hello World


i=4+1=5
55 <= 5 i.e true
Hello World


i=5+1=6
66 <= 5 i.e false

When the value of i reaches 6 then the condition i <= 5 becomes false so the loop breaks and reaches to the end of program.

Similarly to print numbers from 1 to 5 the loop changes to :

#include<stdio.h>
int main(){
     int i; //here i is used as the counter in loop 
     for( i = 1; i <= 5 ; i++)
     {
           printf("%d \t",i);
     } 
     return 0;
}

The output of the above program will be

1    2    3    4    5
while loop

while loop is also similar to for loop where statement is executed until condition is false.

Syntax of while loop :

while(condition){
//statement
}

A simple program to print numbers from to 5 using while loop is

#include <stdio.h>
int main()
{
    int a ;
    a = 1;
    while(a<=5){
        printf("the value of a is %d\n",a);
        a++;
    }
    return 0;
}


The output of the above program is :
the value of a is 1                                                                                                           
the value of a is 2                                                                                                           
the value of a is 3                                                                                                           
the value of a is 4                                                                                                           
the value of a is 5
Do while loop

DO WHILE is a loop where statement is executed at least once even if the condition is false.

Syntax of do ..while loop

do{
   //statements
}while(condition);

Example :

#include <stdio.h>
int main()
{
    int a ;
    a = 1;
    do{
        printf("the value of a is %d\n",a);
        a++;
    }
    while(a==0);
    return 0;
}

The output of the following code will be :

the value of a is 1

In the above case the condition (a==0) is never met but there is output with the value of a . Because in case of do while the statement is executed once before the condition is met or even if the condition is false.

Share this on facebook

Subscribe to our Youtube Channel

Leave a comment

Your email address will not be published. Required fields are marked *