Explaining Do While Loop with Example

Explaining Do While Loop with Example
Techiio-author
Written by Shuvhojit DebFebruary 17, 2022
10 min read
C
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Techiio-author
Shuvhojit Deb

Full Stack Developer

In this blog, we'll discuss the Do While Loop in C.

About Do While Loop

The do-while loop is a post-tested loop. Using the do-while loop, we can repeat the execution of several parts of the statements. The do-while loop is mainly used in the case where we need to execute the loop at least once. The do-while loop is mostly used in menu-driven programs where the termination condition depends upon the end-user.

Syntax:

The syntax of the C language do-while loop is given below:

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

Flow Diagram

blogpost

Examples of Do While Loop

Example-1:

The below example here is to print natural numbers within 10 from 1 number with the do while loop.

Code:-

#include <stdio.h>
int main() {
int i=1; //assisning number 1 to i variable to start the natural numbers
printf("Hi This is pavan.. WELCOME. Here I m Printing Natural Numbers:\n");
do{
printf("%d",i);//printing i variable's value
printf(",");
i=i+1; // assigning incrementation to the i variable
}while(i<10); //loop with the condition
printf("\n"); //For printing the line break
return 0;
}

Output:-

blogpost

Example-2:

Switch case listed do while program to print some specific text based on the options list which are embedded/showing in the program in the terminal/compiler when executing it.

Code:-

#include<stdio.h>
#include<stdlib.h>
void main ()
{
char c1;
int choice1,dummy1;
do{
printf("\n1. Print Hello Pavan\n2. Print C Language\n3. Exit\n");
scanf("%d",&choice1);
switch(choice1)
{
case 1 :
printf("Hello Pavan\n");
break;
case 2:
printf("C Language\n");
break;
case 3:
exit(0);
break;
default:
printf("Atleast Now enter a valid choice/option");
}
printf("If you want to enter again/more?");
scanf("%d",&dummy1);
scanf("%c",&c1);
}while(c1=='y');
}

Output:-

blogpost

Example-3:

This is the C Program to print the table of the number which gave as input to the terminal/compiler by the user of the System. Here you can print any kind of table with up to the 10 multiples of the user input number. kindly give it a try and know how the table’s program is working using do while. even though if the condition in the do while is incorrect/false the program inside the loop will be executed just once without any error/any other.

Code:-

#include<stdio.h>
int main(){
int i1=1,number1=0;
printf("Enter number to print its table : ");
scanf("%d",&number1);
printf("========================\n");
do{
printf("%d X ",number1);
printf("%d = ",i1);
printf("%d \n",(number1*i1));
i1++;
}while(i1<=10);
printf("========================\n");
return 0;
}

Output:-

blogpost

Example-4:

Below C Program is to print the sum of natural numbers using do while loop in my way. Check it you will get a small idea to build many big projects of programming in the future.

Code:-

#include<stdio.h>
int main(){
int i1=1,number2=0,number1,a1;
printf("Enter number to print sum of the natural numbers in my way : ");
scanf("%d",&number1);
printf("========================\n");
do{
printf("%d \n",i1);
number2=number2+i1;
a1=number2;
a1=a1+number2;
i1++;
}while(i1<=number1);
printf("========================\n");
printf("Sum of the above natural numbers ==> %d",number2);
printf("\n========================\n");
return 0;
}

Output:-

blogpost

Example-5:

Infinite Loop program in do while loop syntax of C programming language.

Code:-

#include <stdio.h>
int main(){
int i=1;
do{
printf("%d.",i);
// prints numbers from 1
printf("pavan kumar sake ");
// prints pavan kumar sake
i=i+1;
//incrementing the I value
}while(1);  //it is true every time so the statements inside will be executed everytime
}

Output:-

blogpost

Example-6:

The below example of the C Syntax Program will Print natural numbers, odd numbers, prime numbers, and its sum in a well-illustrated way.

Code:-

#include<stdio.h>
#include<conio.h>
int main()
{
int nums1=1,nums2,nums3=1,nums4=0,nums5=0, nums6=0, nums7=1;  //initializing the variable
printf("List of Even Numbers \n");
do           //do-while loop
{
printf("%d ",2*nums1);
nums4=nums4+(2*nums1);
nums1++;                            //incrementing operation
}while(nums1<=10);
printf("\n");
printf("Sum of Even numbers : %d \n",nums4);
printf("\n");
printf("List of Odd Numbers \n");
do           //do-while loop
{
nums2= (2*nums3)+1;
nums5 = nums5+nums2;
printf("%d ",nums2);
nums3++;                            //incrementing operation
}while(nums3<=10);
printf("\n");
printf("Sum of Odd numbers : %d \n",nums5);
printf("\n");
printf("List of 1st 10 Natural Numbers \n");
do           //do-while loop
{
nums6 = nums6+nums7;
printf("%d ",nums7);
nums7++;                            //incrementing operation
}while(nums7<=10);
printf("\n");
printf("Sum of 1st 10 Natural numbers : %d \n",nums6);
return 0;
}

Output:-

blogpost

Example-7:

This is the example to print the perfect numbers using DO WHILE program with C Language syntax.

Code:-

#include<stdio.h>
int main()
{
int n,k,l;
printf("Enter how many perfect nums you want to print:: ");
scanf("%d",&n);
int c=0;
int i=1;
do{
l=0;
for(k=1;k<i;k++){
if (i%k==0){
l=l+k;
}
}
if(i==l){
printf("\n %d is a perfect number.\n",i);
c=c+1;
}
if(c==n){
break;
}
i=i+1;
}while(i>0);
return 0;
getchar();
}

Output:-

blogpost

Example-8:

Basic simple calculator program using do while and switch case condition. Check the syntax. Everything is mostly simple in the do while c program which is listed below.

Code:-

#include <stdio.h>
int main()
{
int yes1;
int a1, b1, c1, choice1;
yes1 = 1;
do
{
printf("Enter 1st integer: ");
scanf("%d", &a1);
printf("Enter 2nd integer: ");
scanf("%d", &b1);
printf("\n Add (1), Subtract (2), Multiply (3), Divide (4) :: ");
scanf("%d", &choice1);
printf("\n");
switch(choice1)
{
case(1):
c1 = a1 + b1;
printf("%d + %d = %d\n", a1, b1, c1);
break;
case(2):
c1 = a1 - b1;
printf("%d - %d = %d\n", a1, b1, c1);
break;
case(3):
c1 = a1 * b1;
printf("%d * %d = %d\n", a1, b1, c1);
break;
case(4):
c1 = a1 / (float)b1;
printf("%d / %d = %d\n", a1, b1, c1);
break;
default:
printf("Incorrect choice. Try again.\n");
}
printf("\nChoose Option \n 1. YES \n 2. NO : ");
scanf("%d", &yes1);
}while(yes1 == 1);
return 0;
}

Output:-

blogpost

C Language
Programming
Do While Loop
Loops in C
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Was this blog helpful?
techiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plan
You must be Logged in to comment
Code Block
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance