C Program to Demonstrate the do-while Loop

The do-while loop in C is a post-test loop: it executes the body first and checks the condition afterward. This guarantees the body runs at least once, regardless of the condition’s initial truth value. This is the key difference from the while loop, which checks the condition before the first iteration and may never execute the body at all.

Loop When condition is checked Minimum iterations Best for
while Before each iteration 0 General loops
do-while After each iteration 1 Input validation, menus
for Before each iteration 0 Counted loops

The original post used main() with no return type (invalid in C89) and broken \n in format strings.

Syntax

do {
    /* body — executes at least once */
} while (condition);   /* semicolon required */

Note the required semicolon after the closing parenthesis — this is unique to do-while; while and for have no trailing semicolon on the loop header.

C Program: do-while Demonstration

/* Demonstrate do-while loop: print 1..10 and input validation
 * Compile: gcc -ansi -Wall -Wextra dowhile.c -o dowhile */
#include <stdio.h>

int main(void)
{
    int i, n;

    /* Example 1: print 1 to 10 */
    printf("do-while (1 to 10):\n");
    i = 1;
    do {
        printf("%d ", i);
        i++;
    } while (i <= 10);
    printf("\n");

    /* Example 2: input validation — executes at least once */
    do {
        printf("Enter a positive number: ");
        if (scanf("%d", &n) != 1) n = -1;
    } while (n <= 0);
    printf("You entered: %d\n", n);

    return 0;
}

How to Compile and Run

gcc -ansi -Wall -Wextra dowhile.c -o dowhile
./dowhile

Sample Output

do-while (1 to 10):
1 2 3 4 5 6 7 8 9 10

Enter a positive number: -3
Enter a positive number: 0
Enter a positive number: 5
You entered: 5

Code Explanation

  • Example 1 — counting loopi starts at 1, the body prints it and increments, the condition checks i <= 10 at the end. If i starts at 1 and the condition is initially true, a while loop would give the same result. The do-while shines when the condition may be false from the start (see Example 2).
  • Example 2 — input validation — the prompt is always shown at least once. If the user enters 0 or a negative number, the condition n <= 0 is true and the loop repeats. This is the canonical use case for do-while: you always need at least one prompt before you know whether the input is valid.
  • Semicolon after the condition} while (n <= 0); requires the trailing semicolon. Forgetting it is a compile error. This is a syntactic quirk of do-while that differs from all other loop forms in C.

do-while vs while for Input Validation

/* Using while — need to duplicate the prompt */
printf("Enter a positive number: ");
scanf("%d", &n);
while (n <= 0) {
    printf("Enter a positive number: ");
    scanf("%d", &n);
}

/* Using do-while — single prompt, no duplication */
do {
    printf("Enter a positive number: ");
    scanf("%d", &n);
} while (n <= 0);

The do-while version avoids duplicating the prompt and read, which is cleaner and less error-prone (only one place to change if the prompt text changes).

What This Program Teaches

  • Post-test vs pre-test loops — choosing between while and do-while comes down to whether you need the body to run at least once. If you are unsure the condition is true before the first iteration, use while. If you know you need at least one execution, use do-while.
  • Input validation idiom — the do-while input validation loop is one of the most common patterns in C. It appears in menu-driven programs (always show the menu at least once), retry loops (always attempt the operation once), and parsers (always read at least one token).
  • All three C loop forms are equivalent — any do-while can be rewritten as a while (with a duplicated body) or a for. Choosing the right form is about making intent clear to the reader, not about capability.

Related Programs

Recommended book:
The C Programming Language — Kernighan & Ritchie (India) |
(US)
 | 
C Programming: A Modern Approach — K.N. King (India) |
(US)

Practice what you learned: C Aptitude Questions — or try our C Programming Quiz App on Android.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>