Master C Loops: For, While, and Do‑While Explained with Practical Examples
What Is a Loop in C?
A loop repeats a block of statements until a specified condition becomes false. In C, a loop consists of a body and a control statement that dictates how many times the body executes. Mastering loops is essential for writing efficient and readable C programs.
In this tutorial you’ll discover:
- Definition and purpose of loops in C
- Types of loops: entry‑controlled vs. exit‑controlled
- While loop fundamentals
- Do‑while loop fundamentals
- For loop fundamentals
- Using
breakandcontinue - Choosing the right loop for a given scenario
Types of Loops in C
C loops are classified by the position of their control condition:
- Entry‑controlled loops – the condition is evaluated before the body runs (e.g.,
whileandfor). - Exit‑controlled loops – the condition is evaluated after the body runs (e.g.,
do‑while).
An infinite loop occurs when the loop never satisfies its termination condition. Common causes include missing a termination check or an always‑true condition.
While Loop in C
The while loop is the simplest form of an entry‑controlled loop. Its syntax is:
while (condition) {
statements;
}
The condition is evaluated first; if it is true, the body executes. After each execution, the condition is re‑evaluated until it becomes false.
Example – print numbers 1 to 10:
#include <stdio.h>
int main() {
int num = 1;
while (num <= 10) {
printf("%d\n", num);
num++;
}
return 0;
}
Output:
1 2 3 4 5 6 7 8 9 10
Do‑While Loop in C
The do‑while loop guarantees that the body executes at least once, making it an exit‑controlled loop. Syntax:
do {
statements;
} while (condition);
Example – print the multiplication table of 2 up to 10:
#include <stdio.h>
int main() {
int num = 1;
do {
printf("%d\n", 2 * num);
num++;
} while (num <= 10);
return 0;
}
Output:
2 4 6 8 10 12 14 16 18 20
For Loop in C
The for loop is highly efficient for iterating with a known start, end, and step. Syntax:
for (initialization; condition; increment) {
statements;
}
- The initialization runs once.
- The condition is checked before each iteration.
- The increment updates the loop variable.
Example – print numbers 1 to 10:
#include <stdio.h>
int main() {
for (int number = 1; number <= 10; number++) {
printf("%d\n", number);
}
return 0;
}
Output:
1 2 3 4 5 6 7 8 9 10
Complex for loops can include multiple expressions and even empty components:
for (int i = 0, j = num; i < j; i++, j--) {
// statements
}
Nested loops are common, especially for multidimensional arrays. Example – multiplication tables:
#include <stdio.h>
int main() {
int i, j;
int table = 2;
int max = 5;
for (i = 1; i <= table; i++) {
for (j = 0; j <= max; j++) {
printf("%d x %d = %d\n", i, j, i * j);
}
printf("\n");
}
return 0;
}
Output:
1 x 0 = 0 1 x 1 = 1 1 x 2 = 2 1 x 3 = 3 1 x 4 = 4 1 x 5 = 5 2 x 0 = 0 2 x 1 = 2 2 x 2 = 4 2 x 3 = 6 2 x 4 = 8 2 x 5 = 10
Break Statement in C
The break keyword exits the nearest enclosing loop or switch statement immediately. Example – stop a while loop when a value reaches 3:
#include <stdio.h>
int main() {
int num = 5;
while (num > 0) {
if (num == 3)
break;
printf("%d\n", num);
num--;
}
return 0;
}
Output:
5 4
Continue Statement in C
The continue keyword skips the rest of the current iteration and proceeds to the next loop cycle. Example – skip printing the number 5:
#include <stdio.h>
int main() {
int nb = 7;
while (nb > 0) {
nb--;
if (nb == 5)
continue;
printf("%d\n", nb);
}
return 0;
}
Output:
6 4 3 2 1
Choosing the Right Loop
Decide based on when the condition should be evaluated:
- Pre‑test required – use
whileorfor. - Post‑test required – use
do‑while.
Summary
- A loop repeats a block of code until its condition becomes false.
- C offers three primary loops:
while,do‑while, andfor. - Entry‑controlled loops evaluate the condition before each iteration; exit‑controlled loops evaluate after.
- Use
breakto terminate loops early andcontinueto skip iterations. - Choose the loop that best matches the control‑flow requirements of your algorithm.
C Language
- Mastering C# While and Do‑While Loops: Syntax, Examples, and Best Practices
- Mastering C# For Loops: Syntax, Flow, and Practical Examples
- Master C++ For Loops: A Step-by-Step Guide
- Mastering the C for Loop: Syntax, Mechanics, and Practical Examples
- Mastering While and Do‑While Loops in C: Practical Examples
- Mastering Python's While Loop: Syntax, Examples, and Best Practices
- C# Control Flow: IF, SWITCH, FOR, WHILE – Practical Examples & Explanations
- Master Python Loops: For, While, Break, Continue, and Enumerate Explained
- Mastering C Loops: A Comprehensive Guide to Repetition Control
- Master C# Loops: Efficient Code Repetition Techniques