CHAPTER 11 / 26
C While Loop
Learn how to repeat code while a condition remains true.
01 — Syntax
while (condition) {\n // code\n}02 — Example
#include <stdio.h>
int main() {
int i = 1;
while (i <= 5) {
printf("%d ", i);
i++;
}
return 0;
}03 — Important Points
- Make sure the loop condition can eventually become false.
- Update the loop variable.
04 — Common Mistakes
- Creating an infinite loop accidentally.
- Forgetting to update the control variable.
05 — Real-World Use
Loops are essential for processing collections, repeated tasks and streams of data.
06 — Practice
Create a small program that demonstrates the main idea of C While Loop.