🔄 while Loop in C

🔄 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 break to 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.


Keywords: while loop in c, c loop examples, infinite loop c, c programming while syntax, how while works in c Tags: c language, while loop, loop in c, beginner programming, control structure, c examples

একটি মন্তব্য পোস্ট করুন