while Loop — Condition Checked First
A while loop repeats a block of code as long as a condition is true. Before every iteration, the condition is checked. If it is false from the very start, the body never runs at all.
- Best for: when you do not know exactly how many times to repeat — only when to stop
- Condition checked: BEFORE the body runs each time
- Minimum runs: 0 — if condition starts false, body never executes
while loop — flowchart
#include <stdio.h> int main() { int n = 0; // Runs exactly 10 times: n = 0,1,2,...,9 while (n < 10) { printf("n = %d\n", n); n++; // IMPORTANT: update n or loop runs forever! } printf("Loop ended. n = %d\n", n); return 0; }
n = 0 n = 1 n = 2 ... n = 9 Loop ended. n = 10
How the while loop works — step by step:
- Check: Is
n < 10? If yes → run body. If no → exit loop. - Run body: print n, then n++ (n becomes 1)
- Go back to check: Is
1 < 10? Yes → run body again - Repeat until n = 10: Is
10 < 10? No → exit loop
If you forget
n++, the condition stays true forever — infinite loop.
The program freezes. Always make sure something inside the loop moves toward making the condition false.
Infinite while Loop — while(1)
A while(1) loop runs forever because the condition is always true (1). This is intentional in many real programs — servers, game loops, and menus use infinite loops on purpose, controlled by a break statement inside.
while(1)— 1 is always truthy → loops foreverwhile(0)— 0 is always false → body never runs- Any non-zero value in C is considered true
#include <stdio.h> int main() { // This loop NEVER stops on its own while (1) { printf("This loop will run forever.\n"); } return 0; // never reached! }
#include <stdio.h> int main() { int count = 0; // Start with "loop forever" while (1) { count++; printf("Loop iteration: %d\n", count); // break exits when we decide to stop if (count == 5) { printf("Stopping at 5!\n"); break; } } printf("After loop. count = %d\n", count); return 0; }
Loop iteration: 1 Loop iteration: 2 Loop iteration: 3 Loop iteration: 4 Loop iteration: 5 Stopping at 5! After loop. count = 5
Server programs that keep listening for connections, game loops that keep running until the player quits, embedded systems (microcontrollers) that must run forever. The
break statement is how you exit them.
do-while Loop — Body Runs First
The do-while loop is the opposite of while — it runs the body first, then checks the condition. This means the body always executes at least once, even if the condition is false from the start.
- Syntax:
do { body; } while (condition);— note the semicolon at the end! - Condition checked: AFTER the body runs
- Minimum runs: 1 — always runs the body once
- Best for: menus, input validation — things that must show at least once
do-while loop — flowchart (body runs BEFORE check)
#include <stdio.h> int main() { // Initialization int i = 0; do { // Loop body executes FIRST printf("%d ", i); // Update expression i++; } while (i <= 10); // condition checked AFTER body ← semicolon! printf("\nDone!\n"); return 0; }
0 1 2 3 4 5 6 7 8 9 10 Done!
int n = 10; // condition false from start while (n < 5) { printf("runs\n"); } // prints NOTHING
int n = 10; // body runs BEFORE check do { printf("runs\n"); } while (n < 5); // prints "runs" ONCE
#include <stdio.h> int main() { // do-while version of an infinite loop do { printf("This loop will run forever.\n"); } while (1); // condition 1 = always true return 0; }
Use do-while when the action must happen at least once — menus, asking for user input, game rounds.
Use while when you want to skip entirely if the condition is already false — reading files, processing data.
} while (condition); — the semicolon at the end is mandatory in do-while.Without it you get a compiler error. This is different from a regular while loop which has no semicolon.
break — Exit the Loop Immediately
The break directive halts the loop immediately — no more iterations happen. Execution jumps to the first line after the closing } of the loop. It works inside all three loop types: for, while, and do-while.
- Exits the innermost loop only — if nested, only the inner loop breaks
- Also used in
switchstatements to prevent fall-through - The code after the loop continues normally
break — stops loop at iteration 5
#include <stdio.h> int main() { int n = 0; while (1) { // infinite loop — but break will stop it n++; if (n == 10) { printf("Reached 10 — breaking!\n"); break; // exit the while loop immediately } printf("n = %d\n", n); } printf("After loop. n = %d\n", n); return 0; }
n = 1 n = 2 n = 3 n = 4 n = 5 n = 6 n = 7 n = 8 n = 9 Reached 10 — breaking! After loop. n = 10
#include <stdio.h> int main() { int arr[] = {3, 7, 15, 2, 9, 42, 6}; int target = 9; for (int i = 0; i < 7; i++) { printf("Checking arr[%d] = %d\n", i, arr[i]); if (arr[i] == target) { printf("Found %d at index %d!\n", target, i); break; // stop — no need to check the rest } } return 0; }
Checking arr[0] = 3 Checking arr[1] = 7 Checking arr[2] = 15 Checking arr[3] = 2 Checking arr[4] = 9 Found 9 at index 4! ← arr[5]=42 and arr[6]=6 are never checked
continue — Skip This Iteration Only
The continue directive skips the rest of the current iteration and jumps immediately to the next one. The loop does NOT exit — it keeps going. Only the current pass is interrupted.
- In a
whileloop — jumps back to the condition check - In a
forloop — jumps to the update step (i++), then checks condition - Useful for skipping unwanted values while keeping the loop running
continue — skips odd numbers, prints only even
#include <stdio.h> int main() { int n = 0; while (n < 10) { n++; // If n is odd — skip the printf below if (n % 2 == 1) { continue; // jump back to while(n < 10) check } // Only reaches here if n is even printf("The number %d is even.\n", n); } return 0; }
The number 2 is even. The number 4 is even. The number 6 is even. The number 8 is even. The number 10 is even.
#include <stdio.h> int main() { printf("Numbers 1-15 (skipping multiples of 3):\n"); for (int i = 1; i <= 15; i++) { if (i % 3 == 0) { continue; // skip 3, 6, 9, 12, 15 — jump to i++ } printf("%d ", i); } printf("\n"); return 0; }
Numbers 1-15 (skipping multiples of 3): 1 2 4 5 7 8 10 11 13 14
break → exits the loop completely. No more iterations.continue → skips the current pass only. Loop continues with next iteration.Think of it: break = quit the game. continue = skip this round, play next round.
Full Comparison — All Loop Types
| Feature | for | while | do-while |
|---|---|---|---|
| Condition checked | Before each run | Before each run | After each run |
| Minimum executions | 0 | 0 | 1 always |
| Best for | Known count (1 to 10) | Unknown count (read until 0) | Menus, must run once |
| Infinite version | for(;;) | while(1) | do{} while(1); |
| Semicolon after? | No | No | Yes — required! |
| break works? | Yes | Yes | Yes |
| continue works? | Yes (goes to i++) | Yes (goes to condition) | Yes (goes to condition) |
Quick Quiz
What is the minimum number of times a while loop body can execute?
What is the key difference between while and do-while?
What does break do inside a while loop?
In this code — what gets printed?while(n < 10) { n++; if(n%2==1){ continue; } printf("%d ", n); }
Which is required in do-while that regular while does NOT need?
Lesson Checklist
- I understand while checks condition BEFORE running body
- I know while loop minimum runs = 0 (may never execute)
- I understand while(1) is intentional infinite loop
- I understand do-while checks condition AFTER running body
- I know do-while always runs at least once
- I remember the semicolon at end of do-while
- I know break exits the loop completely immediately
- I know continue skips current iteration only — loop continues
- I can use break with while(1) to make a controlled infinite loop
- I completed the quiz