EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

Do While Loop in C

Home » Software Development » Software Development Tutorials » C Programming Tutorial » Do While Loop in C

Do While Loop in C

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:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

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:

Do While Loop in C

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.

Popular Course in this category
C Programming Training (3 Courses, 5 Project)3 Online Courses | 5 Hands-on Projects | 34+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (5,617 ratings)
Course Price

View Course

Related Courses
C++ Training (4 Courses, 5 Projects, 4 Quizzes)Java Training (40 Courses, 29 Projects, 4 Quizzes)

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:

Do While Loop in C-1.1

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:

Do While Loop in C-1.2

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:

Do While Loop in C-1.3

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:

Do While Loop in C-1.4

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:

Do While Loop in C-1.5

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:

Output-1.6

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:

Output-1.7

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:

Output-1.8

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 –

  1. do While Loop in JavaScript
  2. Do While Loop in Python
  3. do While Loop in Java
  4. PHP Do While Loop

C Programming Training (3 Courses, 5 Project)

3 Online Courses

5 Hands-on Projects

34+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
C Programming Tutorial
  • Loops
    • Loops in C
    • For Loop in C
    • While Loop in C
    • Do While Loop in C
    • Nested Loop in C
    • Infinite Loop in C 
  • Basic
    • Introduction to C
    • What is C
    • Career in C Programming
    • Advantages of C
    • How to Install C
    • Best C Compilers
    • Data Types in C
    • Variables in C
    • C Keywords
    • C Command
    • Command Line Arguments in C
    • C Literals
    • Constants in C
    • Unsigned Int in C
    • String in C
  • Pointers
    • Pointers in C
    • Null pointer in C
    • Function Pointer in C
    • Double Pointer in C
    • Void Pointer in C
    • Const Pointer in C
    • Dangling Pointers in C
    • Pointer Arithmetic in C
  • Operators
    • C Operators
    • Arithmetic Operators in C
    • Relational Operators in C
    • Assignment Operators in C
    • Logical Operators in C
    • Conditional Operator in C
    • Modulus Operator in C
    • Ternary Operator in C
    • Address Operator in C
    • Unary Operator in C
    • Operators Precedence in C
    • Left Shift Operator in C
  • Control Statement
    • Control Statements in C
    • If Statement in C
    • If-else Statement in C
    • Else if Statement in C
    • Nested if Statement in C
    • #else in C
    • Structure Padding in C
    • Nested Structure in C
    • Continue Statement in C
    • Break Statement in C
    • Switch Statement in C
    • Goto Statement in C
  • Function
    • Math Functions in C
    • Hashing Function in C
    • Recursive Function in C
    • Power Function in C
    • fputs in C
    • C puts() Function
    • fprintf() in C
    • fseek() in C
    • Stderr in C
    • ASCII Value in C
    • strcat() in C
    • Inline Function in C
    • sizeof() in C
    • Function Prototype in C
    • C ftell()
  • Array
    • Arrays in C Programming
    • 2-D Arrays in C
    • 3D Arrays in C
    • Multidimensional Array in C
    • Array Functions in C
    • Strings Array in C
  • Sorting
    • Sorting in C
    • Heap Sort in C
  • Advanced
    • Constructor in C
    • Encapsulation in C
    • C Storage Classes
    • Static Keyword in C
    • File Handling in C
    • Queue in C
    • Hexadecimal in C 
    • typedef in C
    • Memory Allocation in C
    • Linked List in C
    • Volatile in C
    • Tokens in C
    • Expression in C
    • Regular Expression in C
    • Error Handling in C
    • Types of Errors in C
    • Preprocessor in C
    • Preprocessor Directives in C
    • fscanf() in C
    • #Pragma in C
    • #ifndef in C
    • #undef in C
    • Macros in C
  • C programs
    • Patterns in C Programming
    • Star Patterns in C
    • Number Patterns in C
    • Swapping in C
    • Reverse Number in C
    • Palindrome in C Program
    • Factorial in C
    • Fibonacci Series in C
    • Square Root in C
    • Random Number Generator in C
    • Prime Numbers in C
    • Escape Sequence in C
    • Reverse String in C
    • Leap Year Program in C
    • Anagram Program in C
    • Strong Number in C
    • String Concatenation in C
    • C Programming Matrix Multiplication
    • Decimal to Octal in C
    • Expression Evaluation in C
    • Decimal to Hexadecimal in C
  • Interview question
    • C Programming Interview Questions

Related Courses

C Programming Training Course

C++ Training Course

Java Training Course

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - C Programming Training (3 Courses, 5 Project) Learn More