đ while Loop in C
đ Short Explanation:
The while loop repeats a block of code in C as long as the condition is true. It’s best when the number of iterations is not known in advance.
✅ Syntax
while (condition) {
// code block
}
đ Example: Print 1 to 5
#include <stdio.h>
int main() {
int i = 1;
while (i <= 5) {
printf("%d\\n", i);
i++;
}
return 0;
}
đ§ Output: 1 2 3 4 5
đĄ Common Use Cases
- Reading input until a condition is met
- Looping through dynamic data
- Waiting for a user or sensor input
⚠️ Tips & Best Practices
- Ensure the condition eventually becomes false (avoid infinite loops)
- Use
breakto exit early if needed - Great for data-driven logic or event-based repetition
đ Summary
The while loop is best when you don’t know how many times to run a block of code ahead of time. It checks the condition before every execution.
āĻোāύ āĻŽāύ্āϤāĻŦ্āϝ āύেāĻ:
āĻāĻāĻি āĻŽāύ্āϤāĻŦ্āϝ āĻĒোāϏ্āĻ āĻāϰুāύ