For Loop in C
Loop is a very important concept in programming. It is used to repeat a block of code, for a specified number of times.
For Loop in C
For loop in C is used to iterate over a collection of items. The basic syntax of for loop is as follows:
for ( initialization; condition; updateStatement ) {
// code to be executed
}
The initialization can be any expression, or just a variable. It is only executed once. For example,
// code to be executed
}
int i = 0;
// or
i;
The condition is an expression that evaluates to true or false. The for loop stops when the condition is false and the control is passed to the next statement. It is tested before every execution. For example,
// or
i;
i < 10;
The update statement is an expression that is executed after each iteration. It can be used to increase/decrease the value of a particular variable that is used in the condition. For example,
i = i + 1;
// or
i ++;
The above examples constitute a for loop as,
// or
i ++;
for ( int i = 0; i < 10; i = i ++ ) {
// code to be executed
}
// code to be executed
}
Let's have a look at an example
C
#include <stdio.h>
int main()
{
int i = 0;
for ( i; i < 10; i ++ ) {
printf("i = %d\n", i);
}
return 0;
}
int main()
{
int i = 0;
for ( i; i < 10; i ++ ) {
printf("i = %d\n", i);
}
return 0;
}
Output
i = 0
i = 1
i = 2
i = 3
i = 4
i = 5
i = 6
i = 7
i = 8
i = 9
i = 1
i = 2
i = 3
i = 4
i = 5
i = 6
i = 7
i = 8
i = 9
Explanation
Initially, the value of i is 0. It could also be initialized at the for loop header, without declaring at the top.
for ( int i = 0; i < 10; i ++ )
The condition is then checked, and clearly 0 is less than 10, the block is then executed once. Then, the value of i is updated according to the update statement, which means i = 1. The value of i is then checked again, and the block is executed again. This process continues until i = 10.
Let's have a look at another example
C
#include <stdio.h>
int main()
{
int i, j;
for ( i = 0; i < 5; i ++ ) {
for ( j = 1; j < 4; j ++ ) {
printf("%d", j);
}
printf("\n");
}
return 0;
}
int main()
{
int i, j;
for ( i = 0; i < 5; i ++ ) {
for ( j = 1; j < 4; j ++ ) {
printf("%d", j);
}
printf("\n");
}
return 0;
}
Output
123
123
123
123
123
123
123
123
123
Explanation
The outer for loop is executed 5 times, and the inner for loop is executed 3 times.
Initially, i = 0, and the condition is true, hence the control is passed to the block inside the outer for loop.
Now, j = 1 sets the value of j, and the condition is true, hence the control is passed to the block inside the inner for loop. The value of j is then printed, and updated. The value of j is then checked again, and the block is executed again. This process continues until j = 4.
Now, the value of i is then updated, and the condition is checked again. The block is executed again. This process continues until i = 5.
Initially, i = 0, and the condition is true, hence the control is passed to the block inside the outer for loop.
Now, j = 1 sets the value of j, and the condition is true, hence the control is passed to the block inside the inner for loop. The value of j is then printed, and updated. The value of j is then checked again, and the block is executed again. This process continues until j = 4.
Now, the value of i is then updated, and the condition is checked again. The block is executed again. This process continues until i = 5.
Note to remember!
If the declaration of the variable is done in the for loop header, and not before the for loop, then the variable is defined in the scope of the for loop. Hence, outside the for loop, i will not be defined.Let's verify this with an example
C
#include <stdio.h>
int main()
{
for ( int i = 0; i < 5; i ++ ) {
printf("%d", i);
}
printf("%d", i);
return 0;
}
int main()
{
for ( int i = 0; i < 5; i ++ ) {
printf("%d", i);
}
printf("%d", i);
return 0;
}
Output
Example.c: In function 'main': Example.c:7:14: error: 'i' undeclared (first use in this function) printf("%d", i); ^ Example.c:7:14: note: each undeclared identifier is reported only once for each function it appears in
Explanation
We can clearly see that the variable i is defined in the scope of the for loop, which means it is only available inside the for loop. Hence, outside the for loop, i will not be defined, and causes an error.