🔁 do-while Loop in C

🔁 do-while Loop in C

🔍 Short Explanation:
The do-while loop executes the code block at least once, then repeats it while the condition remains true. The condition is checked after the block runs.


✅ Syntax

do {
    // code block
} while (condition);
  

📋 Example: Print 1 to 5

#include <stdio.h>

int main() {
    int i = 1;

    do {
        printf("%d\\n", i);
        i++;
    } while (i <= 5);

    return 0;
}
  

🧠 Output: 1 2 3 4 5

💡 Common Use Cases

  • Menus where at least one prompt is shown
  • Input validation loops
  • Repeating code that must run once before checking condition

⚠️ Tips & Best Practices

  • Useful when you want the loop to run at least once
  • Remember the semicolon ; after while(condition)
  • Watch out for infinite loops by ensuring condition changes inside loop

📌 Summary

The do-while loop ensures your code runs at least once, then continues repeating as long as the condition is true. Great for menus and input prompts.


Keywords: do while loop in c, c programming do while, c loop examples, do while syntax, do while vs while, beginner c tutorial Tags: c programming, do while loop, control flow, beginner programming, loops in c, input validation

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