If you’re just starting your journey with the C programming language, one of the key topics you’ll come across is loops. Loops are the building blocks of any program that requires repetition.
Whether you want to print a message multiple times, cycle through arrays, or perform tasks until a certain condition is met, loops are your best friend.

In this beginner-friendly guide, we’re going to explore the different types of loops in C - for, while, and do-while. Don’t worry if this sounds a bit technical right now. By the end of this blog, you’ll not only understand what loops are and why we use them, but you’ll also be confident enough to use them in your own C programs.
A loop is a control statement that allows you to execute a block of code multiple times, based on a condition. Instead of writing the same line of code again and again, you can use a loop to repeat it automatically.
This helps in making your code shorter, cleaner, and more efficient.
Imagine wanting to print “Hello” 100 times. Instead of writing 100 print statements, you can simply write a loop that does the job!
There are three main types of loops in C:
For loop
While loop
Do-While loop
Each of these loops works slightly differently and is useful in different situations.
Let’s take a deeper look at each one.
The for loop is one of the most commonly used loops. It is best when you know in advance how many times you want the loop to run.
🔹 Syntax:
Copy Code
for (initialization; condition; update) {
// code to be executed
}🔹 Example:
Copy Code
#include <stdio.h>
int main() {
for (int i = 1; i <= 5; i++) {
printf("%d\n", i);
}
return 0;
}
Output:
1
2
3
4
5🔹 Explanation:
int i = 1 – Initialization: It starts the loop from 1.
i <= 5 – Condition: The loop runs as long as i is less than or equal to 5.
i++ – Update: It increases i by 1 in each loop.
This type of loop is very useful when you are working with arrays, counters, or doing any task that has a clear number of repetitions.
Another example: Printing even numbers from 2 to 10.
Copy Code
for (int i = 2; i <= 10; i += 2) {
printf("%d ", i);
}The while loop is used when you don’t know in advance how many times the loop should run. It keeps executing as long as the condition is true.
🔹 Syntax:
Copy Code
while (condition) {
// code to be executed
}🔹 Example:
Copy Code
#include <stdio.h>
int main() {
int i = 1;
while (i <= 5) {
printf("%d\n", i);
i++;
}
return 0;
}
Output:
1
2
3
4
5This loop is helpful when you’re reading input until the user enters a specific value, or when you're looping through something without knowing its end.
Let’s take another example:
Copy Code
#include <stdio.h>
int main() {
int num;
printf("Enter numbers (0 to stop):\n");
scanf("%d", &num);
while (num != 0) {
printf("You entered: %d\n", num);
scanf("%d", &num);
}
return 0;
}This will continue taking input until the user enters 0.
The do-while loop is like the while loop, except it executes the loop body at least once, even if the condition is false.
🔹 Syntax:
Copy Code
do {
// code to be executed
} while (condition);🔹 Example:
Copy Code
#include <stdio.h>
int main() {
int i = 1;
do {
printf("%d\n", i);
i++;
} while (i <= 5);
return 0;
}
Output:
1
2
3
4
5This loop is useful when you want to ensure that the loop body runs at least once, like in a menu-based program where the menu must be shown at least once.
Another Example:
Copy Code
#include <stdio.h>
int main() {
int choice;
do {
printf("\nMenu:\n1. Print Hello\n2. Exit\nEnter your choice: ");
scanf("%d", &choice);
if (choice == 1) {
printf("Hello\n");
}
} while (choice != 2);
return 0;
}This menu runs at least once no matter what the input is.
| Situation | Best loop |
| You know exactly how many times you need to run the loop | for loop |
| You want to loop until a condition is met (don’t know how many times) | while loop |
| You want the loop body to run at least once, regardless of the condition | do-while loop
|
Sometimes you need one loop inside another. This is called a nested loop.
Example:
Copy Code
#include <stdio.h>
int main() {
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 2; j++) {
printf("i = %d, j = %d\n", i, j);
}
}
return 0;
}Nested loops are often used in matrix operations, printing patterns, or multi-level processing.
1. Forgetting to update variables: Leads to infinite loops.
2. Incorrect loop conditions: Like using = instead of ==.
3. Forgetting curly braces: Especially with multiple statements.
4. Poor variable naming: Use meaningful names to avoid confusion.
5. Not understanding scope: Variables inside loops can have limited scope.
Loops are used in countless situations. Here are just a few:
Printing multiplication tables
Reading files line by line
Searching arrays
Generating patterns
Running games
Validating user input until it’s correct
Let’s do a fun one!
Print a Right-Angled Triangle:
Copy Code
#include <stdio.h>
int main() {
int rows;
printf("Enter number of rows: ");
scanf("%d", &rows);
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++) {
printf("* ");
}
printf("\n");
}
return 0;
}This uses nested loops to print a triangle of stars. Try it with different row values!
Loops can be controlled using special keywords:
break: Exit the loop early
continue: Skip the current iteration and continue to the next
Example with break:
Copy Code
for (int i = 1; i <= 10; i++) {
if (i == 6) break;
printf("%d\n", i);
}Example with continue:
Copy Code
for (int i = 1; i <= 5; i++) {
if (i == 3) continue;
printf("%d\n", i);
}These are useful for improving control over how loops behave.
Loops are everywhere in programming. Whether you’re making a small calculator or a big software project, you’ll find loops helping you automate tasks, repeat logic, and save time. They are not just about syntax, but also about developing logic - how to repeat steps smartly without doing extra work.
It’s okay to make mistakes at first. Many beginners forget a semicolon or write the wrong condition. But every error is a step closer to becoming better.
Start with small problems:
Print the first 10 natural numbers
Calculate the factorial of a number
Display patterns
Sum digits of a number
These are not just beginner questions -they train your brain to think like a programmer.
As you go deeper, you’ll see loops inside algorithms, in data structures, and even behind the scenes of your favorite apps.
If you want to learn more, explore the type of loops in C by Uncodemy. It takes you from basic concepts like loops and conditions to advanced topics like pointers, file handling, and dynamic memory allocation. With real-world examples and assignments, you’ll not only understand theory but also build your problem-solving confidence.
So don’t be afraid of loops. They may seem repetitive, but once you master them, they’ll open doors to everything else in programming. 💻🚀
Personalized learning paths with interactive materials and progress tracking for optimal learning experience.
Explore LMSCreate professional, ATS-optimized resumes tailored for tech roles with intelligent suggestions.
Build ResumeDetailed analysis of how your resume performs in Applicant Tracking Systems with actionable insights.
Check ResumeAI analyzes your code for efficiency, best practices, and bugs with instant feedback.
Try Code ReviewPractice coding in 20+ languages with our cloud-based compiler that works on any device.
Start Coding
TRENDING
BESTSELLER
BESTSELLER
TRENDING
HOT
BESTSELLER
HOT
BESTSELLER
BESTSELLER
HOT
POPULAR