🔁 C Loops Introduction – Repeat Code Efficiently
Loops are powerful tools in C that let you run a block of code multiple times — like telling your computer, “Hey, repeat this until I say stop!” There are three main types of loops in C:
- while loop – checks the condition first, then runs
- for loop – best when you know how many times to repeat
- do-while loop – runs first, then checks the condition
📝 Example: Printing Numbers Using while Loop
This program prints numbers from 1 to 5 using a while
loop.
#include <stdio.h> int main() { int i = 1; while (i <= 5) { printf("Number: %d\n", i); i++; } return 0; }
🎯 Why Use Loops?
- They reduce repetition in code.
- They're essential for processing arrays, files, and user input.
- Less typing, more logic!