← Back to C Course | Chapter 4: Control Flow | Lesson 8 of 11

C do-while Loop

A do-while loop does the steps first and checks afterward, like tasting food before deciding to add salt. It always runs at least once.
Syntax
c
do {
  // body runs at least once
} while (condition);

What is a do-while Loop?

A do-while loop is a post-test loop: it runs its body first and only checks the condition afterward, guaranteeing at least one execution even if the condition would have been false from the very start.

Example: What is a do-while Loop?

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare x and set it to 10
	int x = 10;
	// do-while: run the body once, then repeat while the condition is true
	do {
		// Print formatted text to the screen
		printf("Runs at least once");
	} while (x < 5);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Simple Countdown

You can print a countdown sequence with a do-while loop by decrementing the counter inside the body on each pass, similar to a while loop, but with the guarantee that the first value is always printed at least once.

Example: Simple Countdown

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare i and set it to 5
	int i = 5;
	// do-while: run the body once, then repeat while the condition is true
	do {
		// Print formatted text to the screen
		printf("%d ", i);
		i--;
	} while (i >= 1);
	// Return 0 to signal that the program finished successfully
	return 0;
}

User Input Validation

do-while is well suited for input validation, since you typically need to prompt the user and read their input at least once before you have anything to check -- a plain while loop can't guarantee that first prompt happens.

Example: User Input Validation

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	int value;
	// do-while: run the body once, then repeat while the condition is true
	do {
		sscanf("7", "%d", &value);
	} while (value < 0);
	// Print formatted text to the screen
	printf("%d", value);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Sum Accumulation

Just like a while loop, a do-while loop can accumulate a running total across iterations by updating a sum variable inside its body each time it executes.

Example: Sum Accumulation

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	int i = 1, sum = 0;
	// do-while: run the body once, then repeat while the condition is true
	do {
		sum += i;
		i++;
	} while (i <= 5);
	// Print formatted text to the screen
	printf("%d", sum);
	// Return 0 to signal that the program finished successfully
	return 0;
}

while vs. do-while

The key difference from a while loop is timing: while checks its condition before the first run and may execute zero times, while do-while checks after the first run and always executes at least once.

Example: while vs. do-while

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare x and set it to 10
	int x = 10;
	// Repeat while x < 5 is true
	while (x < 5) {
		// Print formatted text to the screen
		printf("while: never runs\n");
	}
	// do-while: run the body once, then repeat while the condition is true
	do {
		// Print formatted text to the screen
		printf("do-while: runs once");
	} while (x < 5);
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Forgetting the semicolon after while (condition) at the end of a do-while.
  2. Forgetting that the body always runs once even if the condition is false.
  3. Never changing the variable in the condition, creating an infinite loop.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.