do-while Loop in C

Flow Control
If-else Statement in C
Programs on if-else
Switch Case in C
Switch case Programs
Conditional Operator
While loop in C
Do-while loop in C
While vs do-while
For loop in C
Break keyword in C
Continue keyword in C
Break vs Exit in C
Goto keyword in C
☕️ Flow Control Programs
Largest in 3 Numbers
Find Grade of student
Find the absolute value
Vowel or Consonant
Leap Year Program
Simple calculator in C
Check Odd or Even 
Roots of Quadratic Equation
Find Reverse of Number
Factors of a number in C
Generate Multiplication table
Find Power of a Number
Find GCD and LCM
Find factorial of Number
Count Number of Digits
Sum of digits in Number
Sum of N Natural Numbers
Sum of Squares of Natural No.
Find Sum of Odd Numbers
Find the Sum of Series
Find Fibonacci series in C
Sum of the Fibonacci series
Sum until enters +ve numbers
Sum of max 10 no. & Skip -ve
☕️ C Conversion Programs
Celsius to Fahrenheit
Fahrenheit to Celsius
Decimal ↔ Binary
Decimal ↔ Octal
Octal ↔ Binary in C
☕️ Number Programs in C
Prime Number in C
Strong Number in C
Krishnamurthy Number
Neon Number in C
Palindrome number
Perfect Number in C
Armstrong Number
☕️ Pattern Programs in C
Pattern programs in C
Printing pattern using loops
Floyd’s triangle Program
Pascal Triangle Program
Pyramid Star Pattern in C
Diamond Pattern Programs
Half Diamond pattern in C
Print Diamond Pattern
Hollow Diamond Pattern
Diamond Pattern of Numbers

The do-while loop in C is very closely related to the while loop. The do keyword is placed on a line of code at the top of the loop. A block of statements follows it with a test expression after the keyword while, at the bottom of the loop.

It is a post-test loop. The post-test loop executes at least, only then control expression is tested. If the expression is true, the loop repeats. When the expression is false then the loop terminates.

Prerequisites:- while-loop in C

Syntax of do while loop in C

do
{
   statements;
} while(TestExp);

The control directly enters the loop and executes the statements of the loop once, after that it checks the TestExp. If the TestExp is true then statements of the loop again executed else control came out from the loop and executes remaining code of the program.

The statements of the do while loop executes repeatedly until the condition becomes false. Statements may be a single statement or a block of statements.

Notice the semicolon at the end of the do-while loop. Don’t forget to place semicolon at the end of the loop.

Sample Program to demonstrate the do-while loop in C

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

   printf("do-while loop in C programming\n");
   
   do 
   {
     printf("Loop executed %d time\n", n);
     n++;
   } while(n<5);
   
   printf("Control came out from do-while loop");

   return 0;
 }

Output:-

do-while loop in C programming
Loop executed 1 time
Loop executed 2 time
Loop executed 3 time
Loop executed 4 time
Control came out from do-while loop

The statements of the do while loop executes at least once.

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

   printf("do-while loop in C programming\n");
   
   do 
   {
     printf("Loop executed %d time\n", n);
     n++;
   } while(n<0);
   
   printf("Control came out from the do-while loop");

   return 0;
 }

Output:-

do-while loop in C programming
Loop executed 1 time
Control came out from the do-while loop

do while loop Flowchart

do while loop flowchart

Example of do while loop in C

Based on the above points now, we will develop some small programs using do while loop.

C program to print N natural numbers using do while loop

int main()
 {
   int i, n;
   printf("Enter n value: ");
   scanf("%d",&n);
   i=1;
   do {
     printf("%d ",i);
     i++;
   } while(i<=n);
   return 0;
 }

Output:-

Enter n value: 10
1 2 3 4 5 6 7 8 9 10

Menu-driven Program in C using do while

Program description:- Write a menu-driven program to find the area of a circle, triangle, and rectangle. The users will have the choice to choose the option repeatedly.

int main()
 {
   int choice;
   float base, height; //for triangle
   float length, breath; //for rectangle
   float radius; //for circle
   float area;
   
   do
   {
     printf("Choose your option…\n");
     printf("1.Area of the triangle\n");
     printf("2.Area of the rectangle\n");
     printf("3.Area of the circle\n");
     printf("Enter your choice: ");
     scanf("%d", &choice);
     
     switch (choice) {
       case 1:
         printf("Enter the base and height of the triangle: ");
         scanf("%f %f",&base, &height);
         area = base * height;
         printf("Area of triangle = %.2f\n", area);
         break;
       case 2:
         printf("Enter length and breath of the rectangle: ");
         scanf("%f %f", &length, &breath);
         area = length * breath;
         printf("Area of rectangle = %.2f\n", area);
         break;
       case 3:
         printf("Enter radius of circle: ");
         scanf("%f", &radius);
         area = 3.14 * radius * radius;
         printf("Area of circle = %.2f\n",area);
         break;
       default:
         printf("Error! Invalid choice\n");
     }

     printf("\n");
   } while(choice<=3);

   return 0;
 }

Output for the different test-cases:-

Choose your option…
1.Area of the triangle
2.Area of the rectangle
3.Area of the circle
Enter your choice: 1
Enter the base and height of the triangle: 10 20
Area of triangle = 200.00

Choose your option…
1.Area of the triangle
2.Area of the rectangle
3.Area of the circle
Enter your choice: 4
Error! Invalid choice

Nested do while loop in C

The do while loop inside another do while loop is called nested do while loop. Syntax of nested do while loop is given as,

do
 {
   do
   {
     // code 
   } while(condition2);
 } while(condition1);

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Recommended Posts

Flow Control
If-else Statement in C
Programs on if-else
Switch Case in C
Switch case Programs
Conditional Operator
While loop in C
Do-while loop in C
While vs do-while
For loop in C
Break keyword in C
Continue keyword in C
Break vs Exit in C
Goto keyword in C
☕️ Flow Control Programs
Largest among 3 Numbers
Find Grade of student
Find the absolute value
Check Vowel or Consonant
Leap Year Program
Simple calculator in C
Check Odd or Even 
Roots of Quadratic Equation
Find Reverse of Number
Factors of a number in C
Generate Multiplication table
Find Power of a Number
Find GCD and LCM
Find factorial of Number
Count Number of Digits
Sum of digits in Number
Sum of N Natural Numbers
Sum of Squares of Natural No.
Find Sum of Odd Numbers
Find the Sum of Series
Find Fibonacci series in C
Sum of the Fibonacci series
Sum until enters +ve numbers
Sum of max 10 no. & Skip -ve
☕️ C Conversion Programs
Celsius to Fahrenheit
Fahrenheit to Celsius
Decimal ↔ Binary
Decimal ↔ Octal
Octal ↔ Binary in C
☕️ Number Programs in C
Prime Number in C
Strong Number in C
Krishnamurthy Number
Neon Number in C
Palindrome number
Perfect Number in C
Armstrong Number
☕️ Pattern Programs in C
Pattern programs in C
Printing pattern using loops
Floyd’s triangle Program
Pascal Triangle Program
Pyramid Star Pattern in C
Diamond Pattern Programs
Half Diamond pattern in C
Print Diamond Pattern
Hollow Diamond Pattern
Diamond Pattern of Numbers

Leave a Comment

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