Search Result
Details here...

For Loop in C

Previous

If...Else Statement

Next

While 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.

Did you notice?

We skipped the switch conditional statement, for our ease. We'll cover it in the next chapters.

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,
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,
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,
for ( int i = 0; i < 10; i = i ++ ) {
// 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;
}

Output

i = 0
i = 1
i = 2
i = 3
i = 4
i = 5
i = 6
i = 7
i = 8
i = 9

Did you notice?

The for loop immediately terminated when i increased to 10, and the control was passed to the next statement.

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.

Woow!

There can be nested for loops, i.e; a for loop inside a for loop.

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;
}

Output

123
123
123
123
123

Remember

For loops are very important in printing a pattern. We have many examples of it in the example section and CodeXchange.

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.

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;
}

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.

Didn't get what scope is?

We will learn in detail about scopes in the coming chapters.

By darpan.kattel
Previous

If...Else Statement

Next

While Loop in C