C For Loops – Repeat Like a Pro!
Last Updated: September 2025
Want C to repeat tasks without writing the same code again and again? That’s where for
loops come in! They’re simple, powerful, and used everywhere once you start coding in C.
What’s a For Loop?
A for
loop helps you run a block of code multiple times by specifying a starting point, condition, and how to change it after every step. It’s a clean way to repeat tasks without copying and pasting!
Example 1: Looping Through Numbers
#include <stdio.h>
int main() {
for(int i = 0; i < 5; i++) {
printf("%d\n", i);
}
return 0;
}
🔹This will print numbers from 0 to 4. The loop runs 5 times based on the condition!
Example 2: Printing an Array
#include <stdio.h>
int main() {
char* fruits[] = {"Apple", "Banana", "Mango"};
for(int i = 0; i < 3; i++) {
printf("%s\n", fruits[i]);
}
return 0;
}
🔹You can loop through an array and print each element easily!
Example 3: Skipping Items with continue
#include <stdio.h>
int main() {
for(int i = 1; i <= 5; i++) {
if(i == 3) {
continue;
}
printf("%d\n", i);
}
return 0;
}
🔹continue
skips the current step and moves to the next one. Here, it skips printing 3!
Example 4: Stopping Early with break
#include <stdio.h>
int main() {
for(int i = 1; i <= 5; i++) {
if(i == 4) {
break;
}
printf("%d\n", i);
}
return 0;
}
🔹break
stops the loop when a condition is true. Here, it stops before printing 4!
Example 5: Build a Shopping Cart Total Calculator
#include <stdio.h>
int main() {
char* items[] = {"T-shirt", "Jeans", "Sneakers", "Cap"};
int prices[] = {499, 899, 1299, 199};
int total = 0;
for(int i = 0; i < 4; i++) {
printf("Adding %s for ₹%d\n", items[i], prices[i]);
total += prices[i];
}
printf("------------------------\n");
printf("Total amount: ₹%d\n", total);
return 0;
}
🔹This program goes through each item, adds the price, and prints the total at the end!
Why Use For Loops?
- They save time by running repetitive tasks easily.
- They work with arrays, strings, and counters effortlessly.
- You can control exactly how many times you want to repeat!
Practice Time!
Create an array of your favorite games and use a for
loop to print them. Try skipping one using continue
to see how it behaves!