Skip to main content

Beginner-Friendly Topic

Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on.You've got this!

Lesson 4 of 8

while & do-while - Condition-Based Loops

`while` tests the condition **before** each iteration - the body may never execute. `do-while` tests the condition **after** each iteration - the body always executes at least once. `do-while` is the right choice for input validation loops and menu-driven programs where you always want at least one execution.

11 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

while and do-while - When to Use Each

while and do-while - When to Use Each
#include <stdio.h>

int main(void) {
    /* ── while - condition checked BEFORE body ───────── */
    int n = 1;
    while (n <= 8) {
        printf("%d ", n);
        n *= 2;
    }
    printf("
");   /* 1 2 4 8 */

    /* ── Reading until EOF (common file-processing pattern) */
    /* while ((c = fgetc(fp)) != EOF) { process c; } */

    /* ── do-while - condition checked AFTER body ────────
       Body always runs at least ONCE.                    */

    /* Input validation - must execute at least once */
    int choice;
    do {
        printf("Enter 1-3: ");
        if (scanf("%d", &choice) != 1) {
            /* clear bad input */
            while (getchar() != '
') { /* drain stdin */ }
            choice = 0;   /* force re-loop */
        }
    } while (choice < 1 || choice > 3);
    printf("You chose: %d
", choice);

    /* ── while vs do-while comparison ───────────────────
       while: condition checked first → 0 or more executions
       do-while: body runs first     → 1 or more executions
       Use while when 0 iterations is a valid outcome.
       Use do-while when at least 1 iteration is required. */

    /* ── Infinite loop patterns ─────────────────────────
       Both are correct; use the form that matches intent:

       while (1) { if (done) break; }   // condition never tested
       for (;;) { if (done) break; }    // idiom common in systems code
       do { ... } while (1);            // less common, same effect  */

    return 0;
}

Common Mistakes

  • Forgetting to advance the loop variable - `while (n < 10) { printf("%d", n); }` without `n++` is an infinite loop. The variable must be modified somewhere inside the loop body (or in the condition via a function with side effect).
  • Using `while (1)` without a guaranteed exit - every infinite loop should have a clear and reachable `break` condition, or a signal handler that terminates execution. An unbreakable loop is a program bug.
  • Misreading `do-while` - the semicolon after the closing `}` is NOT optional: `do { } while (cond);` - the semicolon is required. Omitting it is a syntax error.

Tip

Tip

Practice while dowhile ConditionBased Loops in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.

Diagram
Loading diagram…

While checks condition before entering; do-while guarantees at least one execution iteration

Practice Task

Note

Practice Task - (1) Write a working example of while dowhile ConditionBased Loops from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.

Quick Quiz

Common Mistake

Warning

A common mistake with while dowhile ConditionBased Loops is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready c code.

Key Takeaways

  • `while` tests the condition **before** each iteration - the body may never execute.
  • Forgetting to advance the loop variable - `while (n < 10) { printf("%d", n); }` without `n++` is an infinite loop. The variable must be modified somewhere inside the loop body (or in the condition via a function with side effect).
  • Using `while (1)` without a guaranteed exit - every infinite loop should have a clear and reachable `break` condition, or a signal handler that terminates execution. An unbreakable loop is a program bug.
  • Misreading `do-while` - the semicolon after the closing `}` is NOT optional: `do { } while (cond);` - the semicolon is required. Omitting it is a syntax error.

Topics in This Module

0/8 done