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

 

Similar post

Hello world in Java

Hello world in C++

Hello world in Python

Hello world in C#

 

Suggested for you

for loop in Java

while loop in Java

for loop in C language

while loop in C language

while loop in cpp language

For loop in Cpp language

Nested for loop in C++ language

Nested for loop in Java language

Nested for  loop in C language

Nested for loop in Python language

 

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

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago

Java program to check odd or even using recursion

Java program to check odd or even using recursion In this tutorial, we discuss a…

2 months ago