While Loop in C
? while Loop in C
The while loop in C is used to repeat a block of code as long as a given condition is true.
? Syntax
#include <stdio.h>int main() { int i = 1; while (i <= 5) { printf("%d\n", i); i++; } return 0;}
? Output:
12345
? Infinite while Loop
while (1) { // runs forever unless you use break}
Use with caution!
?? Common Mistakes
Forgetting to update the loop variable (
i++) ? causes an infinite loopUsing
=instead of==in the condition
? Use Cases
When you don't know how many times to loop ahead of time
Waiting for user input or events