Loops in C – How Programs Repeat Tasks
Heya! 👋 I love helping people, and one of the best ways I do this is by sharing my knowledge and experiences. My journey reflects the power of growth and transformation, and I’m here to document and share it with you.
I started as a pharmacist, practicing at a tertiary hospital in the Northern Region of Ghana. There, I saw firsthand the challenges in healthcare delivery and became fascinated by how technology could offer solutions. This sparked my interest in digital health, a field I believe holds the key to revolutionizing healthcare.
Determined to contribute, I taught myself programming, mastering tools like HTML, CSS, JavaScript, React, PHP, and more. But I craved deeper knowledge and practical experience. That’s when I joined the ALX Software Engineering program, which became a turning point. Spending over 70 hours a week learning, coding, and collaborating, I transitioned fully into tech.
Today, I am a Software Engineer and Digital Health Solutions Architect, building and contributing to innovative digital health solutions. I combine my healthcare expertise with technical skills to create impactful tools that solve real-world problems in health delivery.
Imposter syndrome has been part of my journey, but I’ve learned to embrace it as a sign of growth. Livestreaming my learning process, receiving feedback, and building in public have been crucial in overcoming self-doubt. Each experience has strengthened my belief in showing up, staying consistent, and growing through challenges.
Through this platform, I document my lessons, challenges, and successes to inspire and guide others—whether you’re transitioning careers, exploring digital health, or diving into software development.
I believe in accountability and the value of shared growth. Your feedback keeps me grounded and motivated to continue this journey. Let’s connect, learn, and grow together! 🚀
💡I recently started teaching a course on C programming and as part of my preparation for class, I usually write out lesson notes that I will be teaching from. I have therefore decided to make these into blog posts so anyone else learning C can also benefit from them. Therefore, this lesson is part of the series I am calling: C Programming for Absolute Beginners. I hope you enjoy it. Ask any questions or leave comments if you need further clarification or want to make a suggestion.
So far, your programs can:
Store data
Make decisions
Respond to user input
Now we teach them something powerful:
Repetition.
Because in real programming, we often want to do something more than once. You can access the previous lesson here.
Print numbers from 1 to 10.
Ask the user for input multiple times.
Repeat a calculation.
That’s where loops come in.
What Is a Loop?
A loop allows a block of code to run multiple times.
Instead of writing:
printf("Hello\n");
printf("Hello\n");
printf("Hello\n");
We can write a loop that prints it 3 times automatically.
C has three main loops:
whiledo-whilefor
Let’s go step by step.
1. The while Loop
The while loop runs as long as the condition is true.
Syntax
while (condition) {
// code runs repeatedly
}
Example: Print Numbers 1 to 5
#include <stdio.h>
int main() {
int i = 1;
while (i <= 5) {
printf("%d\n", i);
i++;
}
return 0;
}
How This Works
istarts at 1The loop runs while
i <= 5After printing,
i++increases the valueWhen
ibecomes 6, the loop stops
Important:
If you forget i++, the loop will never stop.
This is called an infinite loop.
2. The do-while Loop
The do-while loop is similar to while.
The difference:
- It runs at least once, even if the condition is false.
Syntax
do {
// code
} while (condition);
Notice the semicolon at the end.
Example
#include <stdio.h>
int main() {
int i = 1;
do {
printf("%d\n", i);
i++;
} while (i <= 5);
return 0;
}
The loop:
Runs first
Then checks the condition
Even if i started at 10, it would still print once.
3. The for Loop
The for loop is commonly used when you know how many times you want to repeat something.
Syntax
for (initialization; condition; update) {
// code
}
Example: Print 1 to 5
#include <stdio.h>
int main() {
for (int i = 1; i <= 5; i++) {
printf("%d\n", i);
}
return 0;
}
This does the same thing as the while loop, but in a shorter form.
Let’s break it down:
int i = 1→ starting pointi <= 5→ conditioni++→ increase after each loop
Everything is in one line.
When Should You Use Each Loop?
Use
whilewhen you don’t know exactly how many times it will run.Use
do-whilewhen the code must run at least once.Use
forwhen you know the number of repetitions.
For beginners, the for loop is usually easier to manage.
Common Beginner Mistakes
Forgetting to update the counter (
i++)Writing wrong conditions
Creating infinite loops
Forgetting semicolon in
do-while
Example of infinite loop:
while (i <= 5) {
printf("%d\n", i);
}
Here, i never changes.
The program will run forever.
Mini Project: Print Even Numbers
Write a program that prints even numbers from 2 to 20.
Example solution:
#include <stdio.h>
int main() {
for (int i = 2; i <= 20; i += 2) {
printf("%d\n", i);
}
return 0;
}
Here, i += 2 increases by 2 each time.
Practice Exercises
Print numbers from 10 down to 1.
Ask the user to enter a number and print its multiplication table.
Use a loop to calculate the sum of numbers from 1 to 100.
Final Thoughts
With loops, your programs can now:
Repeat tasks
Count automatically
Handle multiple inputs
Perform repeated calculations
You now understand:
Variables
Conditions
Input
Loops
That is the foundation of C programming.
Next, we’ll combine everything to build small programs that feel real.