Why use do while loop in C programming?

Do while loop in C has a little difference from other two types of loops which are for loop and while loop. Do while loop is called the post tested loop because it will execute the code inside it at least one time although the condition becomes false. So, we use this loop where we need to execute the code at least once.

Main syntax of do while loop in C language

At first we have to initialize outside the do keyword. Then inside the curly braces we should write our code to execute and increment or decrement bellow it. While keyword should be written after the closing curly brace and then the condition inside the parenthesis. Let’s see the syntax of do while loop in C.

syntax of c do while loop, do while loop in c programming

Now, try to understand do while loop in C through given basic program.

#include <stdio.h>
int main(){
    int i = 1;      // initialization
    do{
        printf("You are learning C do while loop now.\n");
        i++;        // increment section
    }while(i <= 5);     // condition
return 0;
}

Output of this C program will be as follows.

You are learning C do while loop now.
You are learning C do while loop now.
You are learning C do while loop now.
You are learning C do while loop now.
You are learning C do while loop now.

Process returned 0 (0x0)   execution time : 0.027 s
Press any key to continue.

Now, try to analyze this C program.

Here initialization part is i = 1 which means that this do while loop will start from the value of 1 of the variable i.

Then the increment section will increment the value of i after executing the code inside the do while loop each time.

Then the condition part which have i <= 5. This means the program will continue till the value of i will be less than or equal to 5 and the do while loop will break when the value of i will be greater then 5.

Code will be executed at least once

We already have mentioned that the code inside do while loop in C will be executed at least one time although the condition becomes false. Now, let’s see the example bellow to prove this.

#include <stdio.h>
int main(){
    int a = 1;
    do{
        printf("Printing once by do while loop.\n");
        a++;
    }while(a > 10);   // false condition because a is not greater than 10
return 0;
}

Output :

Printing once by do while loop.

Process returned 0 (0x0)   execution time : 0.027 s
Press any key to continue.

Demo programs using do while loop in C

Example of do while loop in C to print all the integers between 1 to 30 using C do while loop.

#include <stdio.h>
int main(){
    int i = 1;
    do{
        printf("%d ", i);
        i++;
    }while(i <= 30);
return 0;
}

Output of this example C program of do while loop.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
Process returned 0 (0x0)   execution time : 0.030 s
Press any key to continue.

Example program of C do while loop– printing all integers which are divisible by 3 and 5 between 1 and 100 using C do while loop.

#include <stdio.h>
int main(){
    int x = 1;

    do{
        if(x % 3 == 0 && x % 5 == 0){
            printf("%d ", x);
        }
      x++;
    }while(x <= 100);
return 0;
}

Output of this program is given bellow.

15 30 45 60 75 90
Process returned 0 (0x0)   execution time : 0.026 s
Press any key to continue.

Example program of C do while loop – Printing all the even number from 1 to 40 and determine their sum using C do while loop.

#include <stdio.h>
int main(){
    int num = 1, sum = 0;

    do{
        if(num % 2 == 0){
            printf("%d ", num);
            sum = sum + num;
        }
      num++;
    }while(num <= 40);
    printf("\n\nSum is = %d\n", sum);
return 0;
}

Let’s see the output of this above C program.

2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40

Sum is = 420

Process returned 0 (0x0)   execution time : 0.027 s
Press any key to continue.

You should try to find some similar problems and their solutions using C do while loop to gather more knowledge about do while loop. Our C practice section can be your good friend to find some similar problem.

Moreover you can also go to our C projects section to get more knowledge about practical use of C programming. We will learn about nested loop in C at the next tutorial. Best of luck!

Previous page:  While loop in C language

Next pageNested loop C language