what is loop in c
A loop in C is a control structure that lets you repeat a block of code multiple times until a certain condition becomes false.
Quick Scoop: What is a Loop in C?
In C, a loop is used when you want to run the same set of statements again and again without writing them repeatedly.
Instead of copying printf("Hello\n"); 100 times, you write it once inside a
loop and let the loop handle repetition.
Common use cases:
- Printing a message many times
- Iterating over arrays
- Performing calculations repeatedly (like sums, averages, searching)
Why Loops Exist (Simple Story)
Imagine you are asked to clap 10 times.
You wouldn’t say “clap” 10 times in your notebook; you’d write:
For count from 1 to 10: clap.
That’s exactly what loops do in C:
- You start from some value.
- You check a condition (keep going or stop).
- You update the value each time (like count++).
Basic Idea of a Loop
Conceptually, every loop in C follows this pattern:
- Initialization – set a starting value (like
int i = 0;).
- Condition – keep looping while some expression is true (like
i < 10).
- Body – the statements that run repeatedly.
- Update – change the loop variable each time (like
i++).
As soon as the condition becomes false, the loop stops and control moves to the next statement after the loop.
Types of Loops in C
C provides three main types of loops.
1. for Loop (Most Popular)
Use a for loop when you know in advance how many times you want to repeat
something.
Basic syntax:
c
for (initialization; condition; update) {
// loop body
}
Example: print numbers from 1 to 5.
c
#include <stdio.h>
int main() {
int i;
for (i = 1; i <= 5; i++) {
printf("%d\n", i);
}
return 0;
}
How it runs:
- Initialize:
i = 1(runs once). - Check condition:
i <= 5. If true, run the body. - After the body, do
i++, then check the condition again. - Stop when
i <= 5is false (i becomes 6).
2. while Loop
Use a while loop when you don’t know how many times you’ll loop, but you
know the stopping condition.
Syntax:
c
while (condition) {
// loop body
}
Example: print numbers from 1 to 5 using while.
c
#include <stdio.h>
int main() {
int count = 1; // initialization
while (count <= 5) { // condition
printf("%d\n", count); // body
count++; // update
}
return 0;
}
- The condition is checked before each iteration.
- If the condition is false at the start, the body may not run even once.
3. do...while Loop
Use do...while when you want the loop body to run at least once ,
regardless of the condition.
Syntax:
c
do {
// loop body
} while (condition);
Example:
c
#include <stdio.h>
int main() {
int i = 2;
do {
printf("Hello World\n");
i++;
} while (i < 1);
return 0;
}
Here, even though i < 1 is false from the beginning, "Hello World" still
prints once, because the body executes before the condition check.
Entry-Controlled vs Exit-Controlled Loops
- Entry-controlled : Condition is checked before entering the loop body.
forandwhileloops.
- Exit-controlled : Condition is checked after the loop body.
do...whileloop.
This affects whether the loop body can execute zero times or must execute at least once.
Mini View: Choosing Which Loop
You can think of loop choice like this:
- Use for :
- When the number of iterations is known.
- Example: printing array elements, counting from 0 to 99.
- Use while :
- When you loop until something happens and you don’t know how many times.
- Example: keep reading input until user enters 0.
- Use do...while :
- When the body must run at least once.
- Example: show a menu, then ask “Do you want to continue?” at the end.
Loop Control Statements
Inside loops, C gives extra control keywords:
break– Immediately exit the loop.continue– Skip the rest of the current iteration and move to the next one.- Occasionally
gotocan be used to jump, but it’s generally discouraged in modern style.
Example with continue in a for loop:
c
for (int i = 1; i <= 10; i++) {
if (i == 5) {
continue; // skip printing 5
}
printf("%d ", i);
}
This prints 1 2 3 4 6 7 8 9 10 (skips 5).
Common Pitfalls (And How to Avoid)
- Infinite loop : When the condition never becomes false, e.g. forgetting to update the loop variable.
- Off-by-one errors : Using
<vs<=incorrectly, leading to one extra or one missing iteration.
- Wrong initialization : Not setting variables before the loop, which can cause unexpected output.
Example infinite loop:
c
int i = 0;
while (i < 5) {
printf("%d\n", i);
// missing i++; // i never changes
}
Tiny Example to Tie It Together
Let’s sum numbers from 1 to 5 using a for loop:
c
#include <stdio.h>
int main() {
int sum = 0;
for (int i = 1; i <= 5; i++) {
sum = sum + i;
}
printf("Sum = %d\n", sum); // Output: Sum = 15
return 0;
}
Here, the loop repeats 5 times, each time adding i to sum, and stops when
i becomes 6.
TL;DR: In C, a loop is a construct that repeats a block of code while a condition is true, using
for,while, ordo...whileto control how and when the repetition happens.
Information gathered from public forums or data available on the internet and portrayed here.