C while
last modified November 14, 2026
The while statement repeats a block of statements for as long as a
condition stays true. It is the most basic loop in C, and the one whose parts
are visible in separate places: the condition sits in the header, while the
initialization comes before the loop and the update belongs somewhere in the
body. This tutorial shows the general form, the counter pattern, loops that run
zero times, loops that end through break, iteration over an array,
the do-while variant, and the endless loop that a forgotten update
produces.
The while statement
The keyword while is followed by a condition in parentheses and by a
statement or a block. The condition is tested before every iteration; when it is
false, the loop ends without running the body again.
while (expression) {
statement(s);
}
Any scalar expression may be used as the condition: a comparison, an integer, a
character or a pointer. Zero means false and every non-zero value means true, so
while (1) repeats forever until something inside the body stops it.
The loop therefore has three parts that the programmer must keep in balance: an
initialization before it, the test in the header, and an update in the body. A
loop whose counter is never updated never ends.
Counting with while
The first example adds the numbers 1 through 9. The counter starts at 1, the condition keeps the loop running while the counter is below 10, and the body adds the counter and then increments it.
#include <stdio.h>
int main(void) {
int i = 1;
int sum = 0;
while (i < 10) {
sum += i;
i++;
}
printf("The sum of 1..9 is %d\n", sum);
return 0;
}
The order of the two statements in the body matters. If i++ came
first, the values added would be 2 through 10 and the result would be 54 rather
than 45. Keeping the update last means the value used in the body is the
one the condition has just tested.
$ gcc -std=c17 -Wall -Wextra -o while_sum while_sum.c $ ./while_sum The sum of 1..9 is 45
Counting down
A while loop can move in either direction. The next program
computes the factorial of 10, the product of all integers from 10 down to 1, by
multiplying and then decrementing the counter.
#include <stdio.h>
int main(void) {
int i = 10;
int factorial = 1;
while (i > 1) {
factorial *= i;
i--;
}
printf("10! = %d\n", factorial);
return 0;
}
The loop stops at i == 1, because multiplying by 1 changes nothing
and the condition i > 1 is false there. factorial is
initialized to 1 rather than 0, since 0 would make every product zero. The
result, 3628800, still fits in a 32-bit int; 13! would not.
$ gcc -std=c17 -Wall -Wextra -o while_factorial while_factorial.c $ ./while_factorial 10! = 3628800
A loop that runs zero times
Because the condition is tested first, a while loop may never run
its body at all. The next program starts with a value that already fails the
test.
#include <stdio.h>
int main(void) {
int n = 10;
int iterations = 0;
while (n < 5) {
iterations++;
n++;
}
printf("n = %d, iterations = %d\n", n, iterations);
return 0;
}
Since 10 < 5 is false, the body is skipped and both variables
keep their initial values. This is why a while loop is a poor
choice when the body must run at least once, such as when it reads the first
value of a sequence: use do-while for that case.
$ gcc -std=c17 -Wall -Wextra -o while_zero_iterations while_zero_iterations.c $ ./while_zero_iterations n = 10, iterations = 0
Leaving the loop with break
When the exit depends on a value computed inside the body, the condition alone
cannot express it. The break statement ends the loop from anywhere
in the body. The next program follows the Collatz sequence of 27: an even number
is halved, an odd number becomes three times itself plus one, and the loop stops
when the value reaches 1.
#include <stdio.h>
int main(void) {
int n = 27;
int steps = 0;
while (1) {
if (n == 1) {
break;
}
if (n % 2 == 0) {
n /= 2;
} else {
n = 3 * n + 1;
}
steps++;
}
printf("reached 1 in %d steps\n", steps);
return 0;
}
while (1) states that the condition is always true, and the
break provides the real exit. The count is 111 steps for the
starting value 27. Writing the test as the loop condition would mean duplicating
the parity logic in the header, which is why the endless form is clearer here.
$ gcc -std=c17 -Wall -Wextra -o while_collatz while_collatz.c $ ./while_collatz reached 1 in 111 steps
Iterating over an array
The counter of a while loop is also an array index. The next example
walks through ten integers and prints their sum. The length is computed from the
array itself, and the index has type size_t, the unsigned type that
sizeof returns.
#include <stdio.h>
int main(void) {
int vals[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
size_t len = sizeof vals / sizeof vals[0];
size_t i = 0;
int sum = 0;
while (i < len) {
sum += vals[i];
i++;
}
printf("The sum is: %d\n", sum);
return 0;
}
The condition must be i < len, not i <= len: the
valid indices of an array of ten elements are 0 through 9, and reading
vals[10] would access memory beyond the array. The loop visits each
element once and ends when the index equals the length.
$ gcc -std=c17 -Wall -Wextra -o while_array while_array.c $ ./while_array The sum is: 55
The do-while loop
A do-while loop puts the condition after the body, so the body runs
once before the first test and at least once in total. It is the natural form for
reading input that ends with a sentinel value, because the program must read a
value before it can decide whether the input is finished.
#include <stdio.h>
int main(void) {
int val;
int sum = 0;
do {
printf("Enter a number (0 to stop):\n");
if (scanf("%d", &val) != 1) {
break;
}
sum += val;
} while (val != 0);
printf("The sum is: %d\n", sum);
return 0;
}
The loop adds every number it reads and stops after the zero has been added,
which changes nothing. The scanf result is checked so that
non-numeric input does not leave val unchanged forever. Note the
semicolon after the closing parenthesis: do-while is the only loop
whose header ends with one.
$ gcc -std=c17 -Wall -Wextra -o do_while do_while.c $ printf '2\n3\n4\n5\n0\n' | ./do_while Enter a number (0 to stop): Enter a number (0 to stop): Enter a number (0 to stop): Enter a number (0 to stop): Enter a number (0 to stop): The sum is: 14
A loop that never ends
Forgetting to update the counter is the classic while bug. The next
program prints the same value forever, because nothing in the body changes
i and the condition never becomes false.
#include <stdio.h>
int main(void) {
int i = 0;
while (i < 5) {
printf("iteration %d\n", i);
}
return 0;
}
The compiler cannot catch this: the program is valid C and behaves exactly as
written. Running it produces an endless stream of "iteration 0" lines, and the
timeout command is needed to stop it. Exit status 124 means that the
program was still running when the time limit expired.
$ gcc -std=c17 -Wall -Wextra -o while_hang while_hang.c $ timeout 2 ./while_hang > /dev/null $ echo $? 124
while or for
Both loops can express the same repetition, and the choice is mostly about where
the bookkeeping belongs. A for loop collects the initialization and
the update in the header, so the whole control of the loop is on one line; that
is the better choice for counting a known number of times or walking an array.
A while loop is clearer when the number of iterations is unknown and
the update is a side effect of the work, such as reading until end of file or
halving a number until it reaches 1. When a while loop contains a
continue, check that the update cannot be skipped, or the loop may
never advance; a for loop runs its update automatically.
Source
cppreference: while statement
cppreference: do-while statement
cppreference: statements
This tutorial covered the while statement in C: its three parts, a
counter that moves up and down, loops that never enter the body, exits through
break, array iteration, the do-while form for input,
and the endless loop caused by a missing update. The counting loop with all of
its bookkeeping in the header is described in C for;
the statements that leave or skip an iteration are covered in
C break and C
continue.
Author
List all C tutorials.