The ability to perform repetitive tasks is a critical feature of any programming language, and C is no exception. In C programming, loops are used to repeatedly execute a block of code until a specified condition is met. This post will guide you through understanding the loops and control statements in C.
For Loop
The for loop is a control structure that allows code to be executed repeatedly. A for loop has three components: initialization, condition, and increment/decrement.
for (initialization; condition; increment/decrement) {
// Code to be executed in each iteration
}
Here's an example of a for loop that prints numbers from 1 to 10 -
for (int i = 1; i <= 10; i++) {
printf("%d\n", i);
}
While Loop
The while loop executes a block of code repeatedly as long as the specified condition remains true. The condition is checked before each iteration, and if it is false initially, the loop is not executed at all.
Syntax -
while (condition) {
// Code to be executed in each iteration
}
Here's an example of a while loop that prints numbers from 1 to 10 -
int i = 1;
while (i <= 10) {
printf("%d\n", i);
i++;
}
Do-While Loop
The do-while loop is similar to the while loop, but it checks the condition at the end of each iteration. This guarantees that the loop body is executed at least once, regardless of the initial condition.
Syntax -
do {
// Code to be executed in each iteration
} while (condition);
Here's an example of a do-while loop that prints numbers from 1 to 10 -
int i = 1;
do {
printf("%d\n", i);
i++;
} while (i <= 10);
Control Statements in C
Control statements help manipulate the flow of execution in loops.
Break Statement -
The break statement is used to prematurely exit a loop. When encountered, it terminates the innermost loop it is within, regardless of the loop's termination condition. Control is transferred to the next statement after the loop.
Example -
for (int i = 0; i < 10; i++) {
if (i == 5) {
break; // Exit the loop when i is equal to 5
}
// Code to be executed in each iteration
}
Continue Statement -
The continue statement is used to skip the remaining statements within the current iteration of a loop and move on to the next iteration. When encountered, it jumps directly to the next iteration of the loop.
Example -
for (int i = 0; i < 10; i++) {
if (i % 2 == 0) {
continue; // Skip even numbers and move to the next iteration
}
// Code to be executed in each iteration for odd numbers
}
Nested Loops
Nested loops are loops within loops, allowing for complex iterations and repetitive tasks. They are especially useful when dealing with multi-dimensional arrays or performing operations on multiple levels.
Example -
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
// Code to be executed in each iteration of nested loops
}
}
By utilizing loops and control statements, programmers can perform repetitive tasks efficiently and control the flow of execution based on specific conditions. These constructs are crucial for writing flexible and powerful programs in C.