C - While Loop
The while
loop repeats a block while a condition remains true.
Learning Objectives
- Write
while
loops with correct initialization and updates.
Prerequisites
Example
#include <stdio.h>
int main(void) {
int i = 3;
while (i > 0) {
printf("%d\n", i);
i--;
}
}
Common Pitfalls
- Forgetting to update the loop variable, causing infinite loops.
Checks for Understanding
- When does the loop body run 0 times?
Show answers
- When the initial condition is false.