🔁 for Loop in C
🔍 Short Explanation:
The for
loop is used in C to repeat a block of code a set number of times. It's compact and best when you know how many times to loop.
✅ Syntax
for (initialization; condition; update) { // code block to execute }
📋 Example: Print 1 to 5
#include <stdio.h> int main() { for (int i = 1; i <= 5; i++) { printf("%d\\n", i); } return 0; }
🧠 Output: 1 2 3 4 5
💡 Common Use Cases
- Looping through numbers
- Accessing array elements
- Running fixed iteration tasks (e.g. 100 times)
⚠️ Tips & Best Practices
- Always check loop limits to avoid infinite loops
- Use
i++
ori += 1
for counting up - You can use
break
andcontinue
inside loops
📌 Summary
The for
loop is a powerful and concise way to repeat tasks in C. Mastering it helps handle loops, arrays, and structured logic efficiently.