Do While Loop: Definition, Example & Results

An error occurred trying to load this video.

Try refreshing the page, or contact customer support.

Coming up next: Loop Control Statements in C: Definition & Examples

You're on a roll. Keep up the good work!

Take Quiz Watch Next Lesson
 Replay
Your next lesson will play in 10 seconds
  • 0:04 Do While Loop
  • 0:26 Syntax
  • 1:28 Flowchart
  • 1:35 Example
  • 2:30 Comparison With…
  • 3:31 Lesson Summary
Save Save Save

Want to watch this again later?

Log in or sign up to add this lesson to a Custom Course.

Log in or Sign up

Timeline
Autoplay
Autoplay
Speed Speed

Recommended Lessons and Courses for You

Lesson Transcript
Instructor: Meghalee Goswami

Meghalee has a masters of computer science and communication engineering.

This lesson explains the use of a do while loop in C programming language. You will also see the comparisons with the while and for loop. Various examples have been included for better understanding.

Do While Loop

If you recall the way the for and while loops work, you will remember that these loop types check for the loop condition at the beginning of the loop. Unless the condition is satisfied the loop will not be executed.

The do while loop checks the condition at the end of the loop. This means that the statements inside the loop body will be executed at least once even if the condition is never true.

Syntax

The syntax of do while loop is as follows:

do {
 /* statement(s); */
 /*increment loop counter*/
} while ( condition );

In case the condition is true, the control goes back to the beginning of the loop. If the condition is false, the control breaks out of the loop. This means that the statements inside the loop are executed before the condition is tested. So the do while loop should be used in all scenarios where the loop body needs to be executed at least once. Often, in a menu-driven program, when actions are supposed to be taken repeatedly based on the user input, we need a do while loop to understand which action the user wants to take. In such cases, the control breaks out of the loop when the user input equivalents to an exit command.

Flowchart

Here is what the do while looks like as a flowchart:


Do while Loop Flowchart


Example

To understand how to implement a do while loop, let's implement a simple example to print values from 10-19.

#include <stdio.h>
int main () {
 int a = 10;
 do {
  printf('value of a: %d\n', a);
  a = a + 1;
 } while( a < 20 );{
 return 0;
}

The output will look like this:

To unlock this lesson you must be a Study.com Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use Study.com

Become a Study.com member and start learning now.
Become a Member  Back
What teachers are saying about Study.com
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 200 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? Study.com has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create an account
Support