C-Program
Loops in C
Last Updated on: 23rd Jan 2025 11:44:16 AM
.Loops in C are control flow statements that allow you to execute a block of code repeatedly. They are essential for automating repetitive tasks and processing collections of data. C provides three main types of loops: for
, while
, and do-while
.
1. The for
Loop
The for
loop is typically used when you know in advance how many times you want to execute a block of code. It has three parts:
for (initialization; condition; increment/decrement) {
// Code to be executed repeatedly
}
initialization
: This part is executed only once at the beginning of the loop. It usually initializes a loop counter variable.condition
: This expression is evaluated before each iteration of the loop. If it is true (non-zero), the loop body is executed. If it is false (zero), the loop terminates.increment/decrement
: This part is executed after each iteration of the loop. It usually updates the loop counter variable.
Example:
#include <stdio.h>
int main() {
for (int i = 1; i <= 5; i++) {
printf("Iteration: %d
", i);
}
return 0;
}
Output:
Iteration: 1
Iteration: 2
Iteration: 3
Iteration: 4
Iteration: 5
In this example:
int i = 1;
initializes the loop counteri
to 1.i <= 5;
is the condition. The loop continues as long asi
is less than or equal to 5.i++;
incrementsi
by 1 after each iteration.
2. The while
Loop
The while
loop executes a block of code as long as a condition is true. The condition is checked before each iteration.
while (condition) {
// Code to be executed repeatedly
}
Example:
#include <stdio.h>
int main() {
int i = 1;
while (i <= 5) {
printf("Iteration: %d
", i);
i++; // Important: Increment i inside the loop
}
return 0;
}
This code produces the same output as the for
loop example. It's crucial to increment i
inside the loop; otherwise, the loop would run infinitely.
3. The do-while
Loop:
The do-while
loop is similar to the while
loop, but the condition is checked after each iteration. This means that the loop body is executed at least once, even if the condition is initially false
do {
// Code to be executed repeatedly
} while (condition);
Example:
#include <stdio.h>
int main() {
int i = 1;
do {
printf("Iteration: %d
", i);
i++;
} while (i <= 5);
return 0;
}
This also produces the same output as the previous examples.
Example demonstrating the "at least once" execution of do-while:
#include <stdio.h>
int main() {
int i = 10; // i is initially greater than 5
do {
printf("This will print once, even though the condition is initially false.
");
} while (i <= 5);
return 0;
}
4. break
and continue
Statements
-
break
: Thebreak
statement is used to immediately exit a loop, regardless of the loop's condition.
#include <stdio.h>
int main() {
for (int i = 1; i <= 10; i++) {
if (i == 5) {
break; // Exit the loop when i is 5
}
printf("Iteration: %d
", i);
}
return 0;
}
Output:
Iteration: 1
Iteration: 2
Iteration: 3
Iteration: 4
continue
: The continue
statement skips the rest of the current iteration of the loop and proceeds to the next iteration.
#include <stdio.h>
int main() {
for (int i = 1; i <= 5; i++) {
if (i == 3) {
continue; // Skip printing when i is 3
}
printf("Iteration: %d
", i);
}
return 0;
}
Output:
Iteration: 1
Iteration: 2
Iteration: 4
Iteration: 5
5. Infinite Loops
An infinite loop is a loop whose condition never becomes false, causing it to run indefinitely. This is usually a bug.
Example of an infinite loop (be careful!):
#include <stdio.h>
int main() {
int i = 1;
while (i <= 5) {
printf("This will loop forever!
"); // Missing i++
}
return 0; // This will never be reached
}
Make sure that your loop conditions eventually become false to avoid infinite loops.
Choosing the Right Loop
- Use
for
when you know the number of iterations in advance. - Use
while
when you don't know the number of iterations but have a condition that determines when to stop. - Use
do-while
when you need the loop body to execute at least once.
Understanding loops is crucial for writing efficient and powerful C programs. They are fundamental building blocks for many algorithms and data processing tasks.