Index
Introduction
Think a for loop is like a train journey. You have a starting point (the initialization), a destination (the condition), and a train that moves forward step by step (the increment/decrement), stopping at each station along the way (the loop body). Just like how a train journey has to continue until it reaches the destination, a for loop continues to run until the condition is no longer true.
In both of these examples, the important thing to remember is that a for loop is a way to repeat a set of instructions multiple times, with different inputs or conditions each time, until a certain goal is met.
for (initialization; condition; increment/decrement) {
statement(s);
}
Here’s How For Loop Works
- Initialization: The loop starts with the
initialization
statement, which sets the initial value of the loop control variable. This statement is executed only once, before the loop starts. - Condition: After the initialization, the loop checks the
condition
statement. This condition is evaluated before each iteration of the loop. If the condition is true, the loop body is executed; otherwise, the loop terminates and the program control passes to the next statement after the loop. - Statement(s): If the condition is true, the
statement(s)
inside the loop body are executed. These statements can be any valid C statements, such as assignments, function calls, or conditional statements. The statements in the loop body are executed repeatedly until the condition becomes false. - Increment/Decrement: At the end of each iteration of the loop, the loop control variable is incremented or decremented according to the
increment/decrement
statement. This statement changes the value of the loop control variable, which is then used to evaluate the condition in the next iteration of the loop. - Condition Check: After the increment/decrement statement, the loop checks the condition again. If the condition is still true, the loop body is executed again, and the process repeats from step 3.
- Termination: If the condition ever becomes false, the loop terminates, and the program control passes to the next statement after the loop.
Here’s an example to illustrate how a for
loop works:
for (int i = 0; i < 5; i++) {
printf("%d ", i);
}
In this example, the loop control variable is i
, which is initialized to 0. The loop will continue as long as i
is less than 5. The printf
statement inside the loop body prints the value of i
to the console. At the end of each iteration of the loop, i
is incremented by 1 using the i++
statement. The loop terminates when i
becomes equal to 5.
So, the for
loop is a powerful tool in C programming that allows you to perform iterative tasks efficiently and conveniently.
Real Life Example:
here’s an example code using for
loop to print the number of papers:
#include <stdio.h>
int main() {
int numPapers = 10;
int i;
printf("Printing the number of papers:\n");
for (i = 1; i <= numPapers; i++) {
printf("%d\n", i);
}
return 0;
}
In this example, we initialize the loop control variable i
to 1, and the loop will continue as long as i
is less than or equal to the number of papers, which is 10. The loop body consists of a single printf
statement that prints the value of i
to the console.
When we run the program, we’ll get the following output:
Printing the number of papers:
1
2
3
4
5
6
7
8
9
10
As you can see, the for
loop is used here to print the numbers 1 to 10, which represents the number of papers.