Iteration is a basic concept in programming that helps you repeat actions without writing the same code multiple times. This is super useful for tasks like going through a list, creating patterns, or handling large amounts of data. In this guide, we’ll explain different types of loops and how they work, step by step, so beginners can easily understand. Let’s get started!
What is Iteration?
Iteration means doing something again and again. For example, if you want to print numbers from 1 to 10, you don’t need to write print
ten times. Instead, you can use a loop to repeat a single print
statement ten times. Loops make your code shorter, cleaner, and faster. There are different types of loops in programming, such as while
loops, do...while
loops, and for
loops. We’ll also talk about special keywords like break
and continue
that control how loops behave.
how many types of loops in C ?
1. what is while
Loop?
A while
loop keeps running as long as a condition is true. It checks the condition first, so it’s called an "entry-controlled loop."
How it Works: Imagine a door guard who only lets people in if they meet a certain condition (like having a ticket). The guard keeps checking each person until the condition fails.
Example:
int count = 0;
while (count < 5) {
printf("Count: %d\n", count);
count++;
}
Here, the loop runs as long as count
is less than 5. The value of count
starts at 0 and increases by 1 each time. When count
reaches 5, the condition becomes false, and the loop stops.
2. what is do...while
Loop?
A do...while
loop is like a while
loop, but it always runs the code at least once because it checks the condition after running the code. This is called an "exit-controlled loop."
How it Works: Think of pouring water into a glass. Even if the glass is full, you’ll pour at least a little water to check. The do...while
loop works the same way.
Example:
int count = 0;
do {
printf("Count: %d\n", count);
count++;
} while (count < 5);
Here, the loop prints count
first and then checks if count
is less than 5. Even if count
starts with a value that doesn’t meet the condition, the code inside the do
block runs at least once.
3. what is for
Loop?
The for
loop is best when you know exactly how many times you want to repeat an action. It has three parts written in one line: initialization, condition, and update.
How it Works: Think of a stopwatch. You start it (initialization), let it run while the condition is true, and stop it after updating it each time.
Example:
for (int i = 0; i < 5; i++) {
printf("i: %d\n", i);
}
This loop starts with i = 0
, runs while i < 5
, and increases i
by 1 after every run. The loop prints 0 to 4, then stops when i
reaches 5.
4. what is Nested Loops?
A nested loop is a loop inside another loop. They’re great for working with grids, tables, or anything that has multiple layers.
How it Works: Think of a clock. The outer loop is like the hour hand, and the inner loop is like the minute hand. For every hour, the minute hand completes a full circle.
Example:
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
printf("i: %d, j: %d\n", i, j);
}
}
This code prints all combinations of i
and j
from 0 to 2. The outer loop controls i
, and the inner loop controls j
.
5. Controlling Loops with break
and continue
Sometimes, you want to stop a loop early or skip some parts. You can do this with break
and continue
.
a. The break
Statement
break
stops the loop immediately, even if the condition hasn’t failed yet.
Example:
for (int i = 0; i < 10; i++) {
if (i == 5) {
break;
}
printf("i: %d\n", i);
}
In this example, the loop stops completely when i
equals 5.
b. The continue
Statement
continue
skips the rest of the code for the current loop iteration and moves to the next one.
Example:
for (int i = 0; i < 10; i++) {
if (i % 2 == 0) {
continue;
}
printf("i: %d\n", i);
}
This loop skips even numbers and prints only odd numbers from 0 to 9.
6. Using the goto
Statement
The goto
statement lets you jump to a specific part of the program using a label. It’s not used often because it can make your code confusing.
Example:
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (i == 1 && j == 1) {
goto exitLoop;
}
printf("i: %d, j: %d\n", i, j);
}
}
exitLoop:
printf("Exited the loop\n");
This code uses goto
to jump out of the nested loops when i == 1
and j == 1
.
7. Choosing the Right Loop
- Use a
while
loop when you don’t know how many times you’ll need to repeat but have a condition to check. - Use a
do...while
loop when you want the code to run at least once, even if the condition is false. - Use a
for
loop when you know the exact number of times to repeat. - Use nested loops for working with grids or multi-layered problems.
Conclusion
Iteration helps you save time and write efficient code by avoiding repetition. By understanding how while
, do...while
, for
, and nested loops work, you can solve a wide range of programming problems. Practice these loops to gain confidence and master them over time!