while and do-while Loop in C (with Programs)
In this lesson: Learn while and do-while loops in C with syntax, differences, dry run and programs like reverse number and sum of digits with output.
while Loop
The while loop repeats a block of code as long as its condition is true. Use it when you don't know in advance how many times the loop should run.
while (condition) { // body // update variable (to avoid infinite loop!) }
The condition is checked before each iteration. If the condition is false from the start, the body never executes.
Example 1: Print 1 to 5
#include <stdio.h> int main() { int i = 1; // Initialization (outside loop) while (i <= 5) { // Condition printf("%d\\n", i); i++; // Update (inside loop) } return 0; }
| Step | i ki value | Condition: i <= 5 | Kya hua | i ab |
|---|---|---|---|---|
| 1 | 1 | 1 <= 5 → true | print 1 | i++ → 2 |
| 2 | 2 | 2 <= 5 → true | print 2 | i++ → 3 |
| 3 | 3 | 3 <= 5 → true | print 3 | i++ → 4 |
| 4 | 4 | 4 <= 5 → true | print 4 | i++ → 5 |
| 5 | 5 | 5 <= 5 → true | print 5 | i++ → 6 |
| 6 | 6 | 6 <= 5 → false | Condition false — loop yahin ruk gaya (STOP) | — |
Loop tab tak chalta hai jab tak i <= 5 sach hai. Har baar i ek badhta hai. Jaise hi i 6 hua, condition jhoothi ho gayi aur loop ruk gaya — isliye 6 print nahi hua.
Example 2: Sum until User Enters 0
#include <stdio.h> int main() { int num, sum = 0; printf("Enter numbers (0 to stop):\\n"); scanf("%d", &num); while (num != 0) { sum += num; scanf("%d", &num); } printf("Total sum = %d\\n", sum); return 0; }
Example 3: Reverse Digits of a Number
#include <stdio.h> int main() { int num, rev = 0; printf("Enter number: "); scanf("%d", &num); while (num != 0) { int digit = num % 10; rev = rev * 10 + digit; num /= 10; } printf("Reversed: %d\\n", rev); return 0; }
do-while Loop
The do-while loop is similar to while, but the body executes at least once because the condition is checked after the body.
do { // body (runs at least ONCE) } while (condition); // Note: semicolon here!
Example: Menu-Driven Program
#include <stdio.h> int main() { int choice; do { printf("\\n1. Add\\n2. Subtract\\n3. Exit\\nEnter choice: "); scanf("%d", &choice); if (choice == 1) printf("Adding...\\n"); else if (choice == 2) printf("Subtracting...\\n"); else if (choice == 3) printf("Goodbye!\\n"); else printf("Invalid choice!\\n"); } while (choice != 3); return 0; }
The do-while loop requires a semicolon after the closing while (condition);. Forgetting it causes a compile error.
while vs do-while vs for
| Feature | for | while | do-while |
|---|---|---|---|
| Condition check | Before (entry-controlled) | Before (entry-controlled) | After (exit-controlled) |
| Min executions | 0 (if condition false) | 0 (if condition false) | 1 (always) |
| Best used when | Count known | Count unknown | Must run at least once |
| Update location | In for() header | Inside body | Inside body |
Infinite Loop
A loop that never ends. Use break to exit from inside.
while (1) { // Always true int x; scanf("%d", &x); if (x == 0) break; // Exit when 0 entered printf("You entered: %d\\n", x); }
Summary
while— condition checked before body; body may never rundo-while— body runs first, condition checked after; runs at least oncedo-whileneeds semicolon:} while (cond);- Use
whilefor unknown count,forfor known count - Always update the loop variable to avoid infinite loops
Authoritative References
- C while statement — cppreference
- C do-while statement — cppreference
- Loop statements — GNU C Language Manual
References reviewed on 14 August 2026. Programs and outputs are original teaching examples.
Frequently Asked Questions
What is the difference between while and do-while loop in C?
while loop checks its condition before running the body, so if the condition is false at the start the body never runs. A do-while loop checks the condition after running the body, so the body always runs at least once. That guaranteed first run is the key difference.When should I use a while loop instead of a for loop?
while loop when you do not know in advance how many times you will repeat — for example reading input until the user types "quit". A for loop suits counting a known number of times, where the counter, condition and step fit neatly on one line.What is an infinite loop in C?
while (1). Sometimes this is intentional (a program that waits for events), but usually it is a bug caused by forgetting to update the variable in the condition.Does a do-while loop always run at least once?
do-while loop tests its condition only after executing the body, the body runs at least one time even if the condition is false from the very beginning. This makes it ideal for menus and input validation.