Cwhile Loop

while Loop

The while loop repeats a block of code for as long as its condition remains true. Unlike a for loop, it does not bundle initialization or an update step into its header — it is the simplest, most general looping construct in C, well suited to situations where you don't know in advance how many iterations you'll need.
Basic Syntax

C
#include <stdio.h>

int main(void) {
    int count = 0;

    while (count < 5) {
        printf("count = %d\n", count);
        count++;
    }
    // Output: count = 0, count = 1, count = 2, count = 3, count = 4

    return 0;
}
The Condition Is Checked Before Every Iteration
A while loop tests its condition before running the body, including the very first time. If the condition is false right from the start, the body never executes at all — a while loop can run zero times.

C
int n = 10;

while (n < 5) {
    printf("this never prints\n");
    n++;
}
printf("loop finished, n is still %d\n", n);
// Output: loop finished, n is still 10
Note
This "check first, maybe never run" behavior is the key difference between while and do-while, which always runs its body at least once. See the dedicated do-while page for a side-by-side comparison.
Common Pattern: Sentinel-Controlled Loops
A sentinel value is a special value that signals "stop". Sentinel-controlled loops keep reading or processing input until that special value appears, which is a very common pattern for interactive programs and simple data feeds.

C
#include <stdio.h>

int main(void) {
    int number;
    int sum = 0;

    printf("Enter numbers to sum, or -1 to stop:\n");
    scanf("%d", &number);

    while (number != -1) {
        sum += number;
        scanf("%d", &number);
    }

    printf("Total: %d\n", sum);
    return 0;
}
Common Pattern: Input Validation
Another everyday use of while is re-prompting a user until they provide acceptable input. The loop keeps running as long as the input remains invalid.

C
#include <stdio.h>

int main(void) {
    int age = -1;

    while (age < 0 || age > 120) {
        printf("Enter a valid age (0-120): ");
        scanf("%d", &age);
    }

    printf("Age accepted: %d\n", age);
    return 0;
}
Avoiding an Infinite while Loop

Because the condition is re-evaluated on every pass, something inside the loop body must eventually make it false, or the loop will run forever. It is easy to forget the update step, especially when refactoring code.

C
// BUGGY: count is never updated, so count < 5 is always true
int count = 0;
while (count < 5) {
    printf("stuck!\n"); // prints forever
}

// FIXED: count is incremented each iteration, so the condition
// eventually becomes false
int count2 = 0;
while (count2 < 5) {
    printf("count2 = %d\n", count2);
    count2++;
}
Key Points
  • while checks its condition before every iteration, including the first -- it can run zero times.

  • Something in the loop body must eventually make the condition false, or the loop runs forever.

  • Sentinel-controlled loops keep processing input until a special stop value appears.

  • Input-validation loops keep re-prompting the user while their input remains invalid.