Back to C Programming
2026-01-316 min read

do-while (C Programming)

Learn do-while (C Programming) step by step with clear examples and exercises.

Why This Matters

The do-while loop is a crucial aspect of C programming that allows you to repeatedly execute a block of code until a specific condition is met. Understanding the do-while loop can help you tackle real-world coding challenges, debug common issues in your C programs, and prepare for exams or interviews. This lesson will guide you through the core concept of the do-while loop, provide a worked example with line-by-line code walkthroughs, discuss common mistakes to avoid, and offer practice questions to test your understanding.

Prerequisites

Before diving into the do-while loop, ensure you have a strong foundation in:

  1. Basic C syntax: variables, operators, and expressions
  2. Control structures: if statements and selection
  3. Looping: while loops and for loops
  4. Functions: defining and calling functions in C
  5. Input/Output (I/O) operations: reading user input using scanf() and printing output using printf()

Core Concept

The do-while loop is a type of control structure that executes a block of code repeatedly until a specific condition is met. Unlike the while loop, which tests the condition before entering the loop, the do-while loop tests the condition after each iteration. This ensures that the loop body will always execute at least once if the initial condition is true.

Here's the syntax for a do-while loop:

do {
// Your code here
} while (condition);

In this syntax, the do keyword indicates the start of the loop, and the curly braces {} enclose the loop body. The while keyword followed by a condition in parentheses determines when the loop should stop.

How the do-while Loop Works

The do-while loop functions as follows:

  1. The program enters the loop and executes the code within the curly braces (the loop body).
  2. After each iteration, the condition inside the parentheses is evaluated. If the condition is true, the loop continues to the next iteration.
  3. If the condition is false, the loop exits, and the program continues with the next statement after the loop.

Here's an example of a do-while loop that counts from 1 to 5:

int i = 1;
do {
printf("i is %d\n", i);
i++; // increment i by 1
} while (i <= 5);

In this example, the loop body prints the value of i and increments it by 1. The condition (i <= 5) ensures that the loop continues as long as i is less than or equal to 5. Once i reaches 6, the condition becomes false, and the loop exits.

Worked Example

Let's explore a more complex example that demonstrates how to use a do-while loop to read user input until they enter a specific value:

#include <stdio.h>

int main() {
int number;
printf("Enter a number between 1 and 10:\n");

do {
scanf("%d", &number);
if (number < 1 || number > 10) {
printf("Invalid input! Please enter a number between 1 and 10.\n");
}
} while (number < 1 || number > 10);

printf("You entered %d, which is valid!\n", number);

return 0;
}

In this example, the do-while loop reads user input using the scanf() function and checks if the input is between 1 and 10. If the input is invalid, the loop prints an error message and continues to the next iteration until a valid number is entered. Once a valid number is entered, the loop exits, and the program prints a success message.

Common Mistakes

Here are some common mistakes to avoid when using do-while loops:

1. Forgetting to initialize the control variable

Always make sure you initialize the control variable (in this case, i) before entering the loop to avoid undefined behavior.

// Incorrect: i is not initialized before the loop
do {
printf("i is %d\n", i);
i++; // increment i by 1
} while (i <= 5);

2. Using an incorrect condition

Ensure that your condition correctly determines when the loop should stop. A common mistake is to use a condition that will never become false, resulting in an endless loop.

// Incorrect: the condition will never be false, creating an endless loop
do {
printf("Hello, World!\n");
} while (1); // 1 is always true

3. Misusing the loop body

The loop body should contain the code that needs to be executed repeatedly. Avoid placing unnecessary or unrelated code within the loop.

// Incorrect: the printf statement is not related to the loop and can be moved outside of it
do {
printf("Hello, World!\n");
int i = 1; // this line is unnecessary and can be moved outside of the loop
} while (true); // creating an endless loop

Common Mistakes - Subheadings

  • Initializing control variables correctly
  • Using correct conditions for loop termination
  • Properly using the loop body
  • Avoiding endless loops

Practice Questions

  1. Write a do-while loop that prints the Fibonacci sequence up to the 20th term.
  2. Create a program that reads user input until they enter a specific word or phrase.
  3. Implement a do-while loop that calculates the factorial of a number entered by the user.
  4. Write a do-while loop that asks the user for their name and greets them until they provide an empty string (indicating they are done).
  5. Create a program that uses a do-while loop to find the smallest positive integer that is not a prime number between 1 and 100.

Common Mistakes

Incorrect Initialization

// Incorrect: i is not initialized before the loop
int i;
do {
printf("i is %d\n", i);
i++; // increment i by 1
} while (i <= 5);

Endless Loop

// Incorrect: the condition will never be false, creating an endless loop
int i = 0;
do {
printf("Hello, World!\n");
i++; // increment i by 1
} while (i < 0);

Misusing Loop Body

// Incorrect: the printf statement is not related to the loop and can be moved outside of it
do {
printf("Hello, World!\n");
int i = 1; // this line is unnecessary and can be moved outside of the loop
} while (true); // creating an endless loop

Common Mistakes - Additional Examples - Subheadings

  • Incorrect Initialization: initializing control variables correctly
  • Endless Loop: using correct conditions for loop termination
  • Misusing Loop Body: properly using the loop body

FAQ

Q: Why use a do-while loop instead of a while loop?

A: The main advantage of using a do-while loop is that it guarantees that the loop body will execute at least once if the initial condition is true, as the condition is tested after each iteration. This can be useful when you need to perform some initial setup or input before checking the condition. In contrast, a while loop may not execute its body if the initial condition is false.

Q: Can I use a do-while loop for infinite loops?

A: Yes, by setting the condition to an expression that will always evaluate to true (like (1 == 1)), you can create an endless loop using a do-while loop. However, be cautious when using endless loops, as they can consume significant resources and make your program unresponsive.

Q: How do I break out of a do-while loop?

A: To exit a do-while loop early, you can use the break statement inside the loop body to immediately terminate the loop. Alternatively, you can set the condition to false and let the loop naturally exit on its next iteration. If you need to break out of multiple nested loops, consider using labeled loops or a combination of break and continue statements.

Q: What is the difference between a do-while loop and a while loop?

A: The main difference between a do-while loop and a while loop lies in when they test their conditions. A do-while loop tests its condition after each iteration, ensuring that the loop body will always execute at least once if the initial condition is true. In contrast, a while loop tests its condition before entering the loop, so it may not execute its body if the initial condition is false. This difference makes do-while loops useful for situations where you need to perform some initial setup or input before checking the condition.