C for loop
The for
loop in C is a control flow statement used for repetitive execution of a block of code. It is particularly useful when the number of iterations is known before entering the loop. The for
loop consists of three main components: initialization, condition, and iteration expression (update). This structure makes it clear and concise, allowing for easy readability and maintenance of the code.
Syntax of the for
Loop
for (initialization; condition; update) {
// Code to execute in each iteration
}
Components of the for
Loop
Initialization: This part is executed once at the beginning of the loop. It is typically used to declare and initialize the loop control variable.
Condition: This is a boolean expression that is evaluated before each iteration. If the condition evaluates to
true
(non-zero), the loop body executes. If it evaluates tofalse
(zero), the loop terminates.Update: This expression is executed after each iteration of the loop. It usually modifies the loop control variable (e.g., incrementing or decrementing it).
Example of a for
Loop
Here is a simple example that demonstrates the use of a for
loop:
#include <stdio.h>
int main() {
// A for loop that prints numbers from 0 to 4
for (int i = 0; i < 5; i++) {
printf("Iteration %d\n", i);
}
return 0;
}
Explanation of the Example
- Initialization:
int i = 0
initializes the loop control variablei
to0
. - Condition:
i < 5
checks ifi
is less than5
. If true, the loop body executes. - Update:
i++
increments the value ofi
by1
after each iteration.
The output of this code will be:
Iteration 0 Iteration 1 Iteration 2 Iteration 3 Iteration 4
Nested for
Loops
You can also nest for
loops, allowing for multi-dimensional iterations. Here’s an example of a nested for
loop:
#include <stdio.h>
int main() {
// Nested for loops to print a multiplication table
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= 5; j++) {
printf("%d\t", i * j); // Multiply i and j
}
printf("\n"); // New line after each row
}
return 0;
}
This example prints a multiplication table from 1 to 5. The output will be:
1 2 3 4 5 2 4 6 8 10 3 6 9 12 15 4 8 12 16 20 5 10 15 20 25
Infinite for
Loop
A for
loop can also create an infinite loop if the condition is always true. For example:
for (;;) {
// This will run indefinitely until interrupted
printf("This loop runs forever.\n");
}
Breaking Out of a for
Loop
You can use the break
statement to exit a for
loop prematurely:
#include <stdio.h>
int main() {
for (int i = 0; i < 10; i++) {
if (i == 5) {
break; // Exit the loop when i is 5
}
printf("%d\n", i);
}
return 0;
}
In this case, the loop will print:
0 1 2 3 4
And will stop when i
equals 5
.
Continuing to the Next Iteration
You can use the continue
statement to skip the current iteration and continue to the next one:
#include <stdio.h>
int main() {
for (int i = 0; i < 5; i++) {
if (i == 2) {
continue; // Skip the iteration when i is 2
}
printf("%d\n", i);
}
return 0;
}
The output will be:
0 1 3 4
In this example, the number 2
is skipped.
Summary
- The
for
loop is ideal for situations where the number of iterations is known in advance. - Its structure (initialization, condition, update) allows for clear and organized code.
- You can use nested
for
loops for multi-dimensional data processing. break
andcontinue
statements allow for more control over the loop's execution flow.
The for
loop is a powerful tool in C programming that helps automate repetitive tasks efficiently.