Updated March 23, 2023
Introduction to Do While Loop in C
DO WHILE loop is the same as WHILE LOOP built-in term of the C Programming Language/Many other Programming Languages but DO WHILE loops execute the Program Statements first then the condition will be checked next. This is the main different thing when we compare with the WHILE LOOP. The condition will be checked first by the WHILE LOOP then the Programming Statements will be executed first. DO WHILE will execute the program at first even if the condition is valid/un-appropriate/False at first.
Syntax:
do{
//Program Statements which are to be executed if the condition of the LOOP is TRUE.
}while(Condition);
Parameters:
- Statements inside of the do while will be executed based on its instruction only if the condition of the loop is true the second time. at first, statements will be executed and printed without checking the loop condition.
- While(Condition): Condition inside a loop is a parameter to run the program if the condition is TRUE else no programming statements will be executed which are inside of it
Flowchart of Do While Loop
The flow chart of do while loop in C are given below:
How do While Loop Works in C?:
The do while loop works based on the condition in the while() parameter but at 1st the program inside of the do while will be executed then the condition is checked. it is the main working difference between the while and the do while program.
Examples of Do While Loop in C
Examples of do while in C programming are given below:
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:
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:
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:
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:
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:
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:
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:
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:
Recommended Articles
This is a guide to Do While Loop in C. Here we discuss the basic concept and parameters of do-while loop in C along with different examples and its code implementation. You may also look at the following articles to learn more –