what is Loop?
A loop is a programming structure used to repeat a set of instructions multiple times until a given condition is met.
Instead of writing the same code again and again, we use loops.
Why do we need loops?
Without loop i we want to print Hello 4 times then:
printf("Hello\n");
printf("Hello\n");
printf("Hello\n");
printf("Hello\n");With loop
for(int i = 1; i <= 4; i++) {
printf("Hello\n");
}- Code becomes shorter
- Easy to read
- Easy to modify
- Saves time
Real-Life Example
- Teacher takes attendance every day
- Alarm rings every morning
- Echo runs on his wheel again and again
All these are loops in real life.
How a Loop Works
Every loop has three main parts:
1. Initialization
→ Starting point
int i = 1;2. Condition
→ Loop runs while condition is true
i <= 53. Update (Increment / Decrement)
→ Changes value to avoid infinite loop
i++Types of Loops in C Language
C has 3 main types of loops:
- For Loop
- While Loop
- Do-While Loop
