While loop

Do while loop in C programming language

Do while  loop in C programming language

In this tutorial, we will discuss do while loop in C programming language

In the C language, there are three types of basic looping statements

for loop

while loop

do while loop

Do while loop inC

do while loop

In the C programming language, do- while loop is used for execution and evaluation of C code repeatedly until the test expression is false. When the do-while loop is executed. The test expression is evaluated until the condition is satisfied. Do while loop is executed at least once before the while part is executed.

Do while loop contains two-part

  • Do part – Do part is a starting point of the do-while loop. do part is always executed.
  • While part– Do part is the processing point of do-while loop.

 

Declaration

The syntax of do while loop

do{
      //statement(s) to be executed
}while(condition);

flow diagram of while loop in Java

Do- while loop in C

How do-while loop works:

According to the above diagram, initially, execution starts and flow of control enters the body of the do-while loop and statement are executed only once.

Then, the test expression is evaluated.

When the test expression is true, this process continues until the test expression becomes false.

When test expression is false, the do-while loop stops the execution and goes to rest.

Program 1

#include <stdio.h>
#include <stdlib.h>

int main()
{
    printf("Do while loop in C\n");
    int i=1;
    do{
        printf("value of i is : %d\n",i);
        i++;

    }while(i<=10);
    getch();
    return 0;
}

 

When the above code is executed, it produces the following results:

Do while loop in C
value of i is : 1
value of i is : 2
value of i is : 3
value of i is : 4
value of i is : 5
value of i is : 6
value of i is : 7
value of i is : 8
value of i is : 9
value of i is : 10

In above the program of C language, it displays 1 to 10 natural numbers

Program 2

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int sum=0,i=1; //initialization variables sum and i
    do{
            sum=sum+i;
        i++; //increment of counter variable in every cycle

    }while(i<=10);
    printf("sum of first 10 nutural numbers are : %d",sum);
    getch();
    return 0;
}

When the above code is executed, it produces the following results:

Sum of the first ten natural numbers is: 55

In this program of C language, it calculates the sum of the first ten natural numbers

Find factorial of given numbers

Program 3

#include <stdio.h>
#include <conio.h>

int main()
{
    int n,i=1,f=1;
    printf("\nEnter the number:");
    scanf("%d",&n);

    do{
        f=f*i;
        i++;
    }while(i<=n);
    printf("\n The factorial of %d is %d",n,f);
}

When the above code is executed, it produces the following results:

Enter the number :6
The factorial of 6 is 720

In this program of C language, it calculates the factorial of given numbers

 

Single dimension array with Do while loop

#include <stdio.h>
#include <stdlib.h>

int main()
{
    printf("Array elements printed here\n");
    int marks []={56,35,76,43,91,58};
    //array index with start with Zero
//So "i" started always with zero

    int i=0;
    do{
        printf("%d\n",marks[i]);
        i++;

    }while(i<=5);
    getch();
    return 0;
}

 

When the above code is executed, it produces the following results:

Array elements printed here
56
35
76
43
91
58

In this program of C language, it displays a single dimension array of elements

Infinitive Do while loop in C language

Program 1

#include <stdio.h>
#include <stdlib.h>

int main()
{
    do{
          printf("this is an infinitive do while loop in C\n");
    }while(1==1);

    getch();
    return 0;
}

When the above code is executed, it produces the following results:

this is an infinitive do while loop in C

this is an infinitive do while loop in C

this is an infinitive do while loop in C

this is an infinitive do while loop in C

this is an infinitive do while loop in C

this is an infinitive do while loop in C

this is an infinitive do while loop in C

…………………….

……………………..

this loop is printed infinite time

 

Suggested for you

Nested while loop in Java

Nested while loop in C++

Nested while loop in C

Nested while loop in Python

While loop in Cpp programming language
Do while loop in Java programming language
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

2 months ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

2 months ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

10 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

10 months ago