Search Placementyogi.com
 

For Loop in C

The for loop in C allows us to execute a set of statements a specific number of times.

Syntax of for loop in C:

for(initialization;condition;iteration)
{
	//body
}

If there is just one statement in the body , the braces are not required.

For Loop Control Flow in Java

Control Flow of for loop in C:

  1. The initialization statement is executed first.It initializes the value of the "loop control variable" which acts as the counter of the for loop. It executes only once at the beginning of the for loop.
  2. The condition statement is executed next and tests the value of the loop control variable and decides whether the body of the for loop can be executed. It can only be a boolean expression. If the condition evaluates to true , the body is executed otherwise the loop terminates and the statement after the for loop is executed.
  3. The body of the for loop is executed next.
  4. Once the control finishes executing the last statement of the body, the control goes to the iteration statement. It updates/modifies the value of the loop control variable/ counter and prepares for the next iteration.
  5. Then the control again enters the condition statement and checks whether the updated control variable still satisfies the condition. 

Thus, the steps 2, 3 and 4 are executed in a loop until the condition evaluates to false.

Example of for loop in C:

#include<stdio.h>
int main()
{
   int i;
   for(i=5;i<10;i++)
   {
        printf("i=%d",i);
   }
        printf("After for loop");
}

Output:

i=5

i=6

i=7

i=8

i=9

After for loop

 

Explanation: 

  • Notice that the variable i is declared inside the initialization statement of the for loop. This means that the scope of "i" is only inside the for loop.
  • First, i is initialized to 5. Then i<10 condition is tested and since it is true, the control enters the body.
  • The statement println is executed and i=5 is printed.
  • Then, the iteration statement (i++) is executed and increments the value of i to 6.
  • This updated value of i is tested again in the condition statement (i<10) , since 6<10 is true, the control again enter the body and prints i=6.
  • This goes on, till i=9 is printed.
  • When the control enters the iteration statement this time, i becomes 10.
  • The condition is tested and i<10 becomes false.
  • The control comes out of the for loop and "After for loop" is printed!
  • Try to go through the above explanation once more to grasp the whole concept thoroughly. This is one of the most important concept and many interview questions are asked from for loop based on the control flow.

Variations of for loop in C

The initialization statement, condition statement and iteration statement are not compulsory, and they can be kept empty.

We can rewrite the above program in these way also:

Variation 1: Without the initialization and iteration statement
#include<stdio.h>
int main()
{
   int i=5;
   for(;i<10;)
   {
        printf("i=%d",i);
        i++;
   }
        printf("After for loop");
}

In the above program, we have intentionally kept the initialization and iteration part empty. But, T achieve this, we had to write the initialization statement before the for loop and had to increment the counter inside the body of the loop.

Variation 2: Without the Condition Statement

In the following program, we have eliminated the condition statement also, but we have to modify the body of the for loop so that the it does not become an infinite loop. This is done by using the break statement inside the for loop body. when the value of i becomes 10 , the break statement terminates the for loop. We will discuss the break statement in the next topic.

#include<stdio.h>
int main()
{
   int i=5;
   for(;;)
   {
        printf("i=%d",i);
        i++;
        if(i==10)
           break;
   }
        printf("After for loop");
}
Variation 3: Infinite loop in C using for 

The following program is an infinite loop as all the three parts of the for loop are kept empty.

#include<stdio.h>
int main()
{
   for(;;)
   {
        printf("inside Infinite Loop");
   }
}
Banner to LogicGuns.com

Hide Page Information
for loop in C