ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

Do While and While Loop in C Programming Language

Updated on July 7, 2018
rajkishor09 profile image

Raj is an ardent coder who loves exploring new technology. He is an IT pro with over a decade of experience in C#, Angular, React, and Vue.

If you are looking for what is loop and why we use loop in C language then you can check my article on that topic. This article is entirely dedicated to do-while loop and its usage. I must tell you that do-while loop is similar to while loop but only one thing differentiate them. I will show that difference with example later.

do-while loop syntax

initialize loop variable;
do
{
	loop statement 1;
	loop statement 2;
	loop statement 3;
	………………….
	loop statement N;
	increment loop variable;
} while(condition);

while loop syntax

initialize loop variable;
while(condition)
{
	loop statement 1;
	loop statement 2;
	loop statement 3;
	………………….
	loop statement N;
	increment loop variable;
}

I have provided syntax of do-while loop along while loop so that we can compare them. As you can see do-while loop starts with “do” keyword and then statement to execute inside loop and then finally condition with “while” keyword. If we compare do-while loop syntax with while loop syntax we can see one big difference. In case of while loop we write condition at the beginning of loop whereas in do-while loop at the end of loop. That difference not limited to syntax, that different also makes them to logically behave differently.

There is one more minor difference in their syntax that is in do-while loop we put semicolon (;) after while statement but in while loop we don’t. This point is just to highlight this minor difference, I saw many people forget to put semicolon in do-while loop and sometimes put semicolon in while loop.

Let’s check one example of do-while loop then we will compare same example with while loop.

Do while loop example

#include<stdio.h>
#include<conio.h>

void main()
{

int i=1;
clrscr();

do
{
	printf("%d : do while loop.\n", i);
	i++;
}
while(i<=10);

getch();
}
Do-while loop output
Do-while loop output | Source

Do while loop example explanation

This is same example we used in while loop in C language tutorial, here, instead of while loop we have do-while loop to achieve same output. For those who are reading this article for the first time and never read while loop tutorial, here is a summary what above program does. Actually above program prints “do while loop” with line no. for 10 times as we have condition (i<=10).

Line no. 10-15 is our do-while loop block which prints message and increments value of “i” by 1. In line no. 10 we have “do” keyword which is beginning of do-while loop. Between line no. 11–14 we have statements which we are going to execute inside loop. In line no. 15 we are providing do-while loop execution condition. Here we are instructing it to loop line no. 12 & 13 for 10 times as condition is (i<=10) and initial value of “i” is 1. Also notice semicolon (;) in line no. 15, its end of do-while loop and guys never forget to put that.

Difference between While and Do-While Loop

As I said earlier that there is logical difference in while loop and do-while loop, I am going to show that in this section. There is only one major logical difference between while and do-while loop and by logical difference I mean their behavior in program (not in syntax). Remember this difference, in case of do-while loop whether provided condition is true and false, statement inside do-while loop will execute at least once. Or in other word, whatever is the do-while loop condition, it will execute its statements at least once. But in while loop it will execute loop statement (statements meant to be execute inside loop) only if condition is true.

This strange behavior of do-while loop is because we check loop condition at the end of loop whereas in while loop first we check loop condition and then loop statement executes if condition holds true. Let me give you one example where we will execute same statement in while loop as well as in do-while loop.

#include<stdio.h>
#include<conio.h>

void main()
{

int i=99;
clrscr();

do
{
	printf("%d : do while loop.\n", i);
	i++;
}
while(i<=10);

i=99; // resetting value to 99.

while(i<=10)
{
	printf("%d : while loop.\n", i);
	i++;
}


getch();
}
Difference between While and Do-While Loop
Difference between While and Do-While Loop | Source

Explanation

So above we have sample program which demonstrate the difference between while and do-while loop. From line no. 10-14, we have while loop and from line 16-21 do-while loop. In line no. 7 we are declaring and initializing variable “i” to 99 and in both loops we have same condition and loop statements.

When we run this program, one would expect no output, only blank screen. Why? Let me tell you, in line no. 7 we are assigning value of “i” to 99 and in both loop (while and do-while loop) we have same condition i.e. “i” less than equal to 10 ( i<=10). As you know, loop condition is not satisfied so all statements written inside loop block will not execute. Logically this is correct. But in case of do-while loop, it will execute loop statement at least once even condition is not satisfied. You can check output for confirmation. There you can see message from do-while loop only. This is the unique behavior of do-while loop, it executes its loop statement and then check for condition. If condition is satisfied then it loops again otherwise it exists from loop.

I hope this article somehow tried to help you understand do-while loop and its difference with while loop.

There are some important points to remember when we use loop in our program. Those points I already discussed in “Point to remember” section of while loop in C language article. Don’t forget to check that article.

Your Opinion

Did this help you to learn Do-While loop?

See results

© 2011 RAJKISHOR SAHU

working

This website uses cookies

As a user in the EEA, your approval is needed on a few things. To provide a better website experience, hubpages.com uses cookies (and other similar technologies) and may collect, process, and share personal data. Please choose which areas of our service you consent to our doing so.

For more information on managing or withdrawing consents and how we handle data, visit our Privacy Policy at: https://corp.maven.io/privacy-policy

Show Details
Necessary
HubPages Device IDThis is used to identify particular browsers or devices when the access the service, and is used for security reasons.
LoginThis is necessary to sign in to the HubPages Service.
Google RecaptchaThis is used to prevent bots and spam. (Privacy Policy)
AkismetThis is used to detect comment spam. (Privacy Policy)
HubPages Google AnalyticsThis is used to provide data on traffic to our website, all personally identifyable data is anonymized. (Privacy Policy)
HubPages Traffic PixelThis is used to collect data on traffic to articles and other pages on our site. Unless you are signed in to a HubPages account, all personally identifiable information is anonymized.
Amazon Web ServicesThis is a cloud services platform that we used to host our service. (Privacy Policy)
CloudflareThis is a cloud CDN service that we use to efficiently deliver files required for our service to operate such as javascript, cascading style sheets, images, and videos. (Privacy Policy)
Google Hosted LibrariesJavascript software libraries such as jQuery are loaded at endpoints on the googleapis.com or gstatic.com domains, for performance and efficiency reasons. (Privacy Policy)
Features
Google Custom SearchThis is feature allows you to search the site. (Privacy Policy)
Google MapsSome articles have Google Maps embedded in them. (Privacy Policy)
Google ChartsThis is used to display charts and graphs on articles and the author center. (Privacy Policy)
Google AdSense Host APIThis service allows you to sign up for or associate a Google AdSense account with HubPages, so that you can earn money from ads on your articles. No data is shared unless you engage with this feature. (Privacy Policy)
Google YouTubeSome articles have YouTube videos embedded in them. (Privacy Policy)
VimeoSome articles have Vimeo videos embedded in them. (Privacy Policy)
PaypalThis is used for a registered author who enrolls in the HubPages Earnings program and requests to be paid via PayPal. No data is shared with Paypal unless you engage with this feature. (Privacy Policy)
Facebook LoginYou can use this to streamline signing up for, or signing in to your Hubpages account. No data is shared with Facebook unless you engage with this feature. (Privacy Policy)
MavenThis supports the Maven widget and search functionality. (Privacy Policy)
Marketing
Google AdSenseThis is an ad network. (Privacy Policy)
Google DoubleClickGoogle provides ad serving technology and runs an ad network. (Privacy Policy)
Index ExchangeThis is an ad network. (Privacy Policy)
SovrnThis is an ad network. (Privacy Policy)
Facebook AdsThis is an ad network. (Privacy Policy)
Amazon Unified Ad MarketplaceThis is an ad network. (Privacy Policy)
AppNexusThis is an ad network. (Privacy Policy)
OpenxThis is an ad network. (Privacy Policy)
Rubicon ProjectThis is an ad network. (Privacy Policy)
TripleLiftThis is an ad network. (Privacy Policy)
Say MediaWe partner with Say Media to deliver ad campaigns on our sites. (Privacy Policy)
Remarketing PixelsWe may use remarketing pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to advertise the HubPages Service to people that have visited our sites.
Conversion Tracking PixelsWe may use conversion tracking pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to identify when an advertisement has successfully resulted in the desired action, such as signing up for the HubPages Service or publishing an article on the HubPages Service.
Statistics
Author Google AnalyticsThis is used to provide traffic data and reports to the authors of articles on the HubPages Service. (Privacy Policy)
ComscoreComScore is a media measurement and analytics company providing marketing data and analytics to enterprises, media and advertising agencies, and publishers. Non-consent will result in ComScore only processing obfuscated personal data. (Privacy Policy)
Amazon Tracking PixelSome articles display amazon products as part of the Amazon Affiliate program, this pixel provides traffic statistics for those products (Privacy Policy)
ClickscoThis is a data management platform studying reader behavior (Privacy Policy)