For loop

 

For loop is an alternative to a while loop. The for loop and while loop is used for the same purpose. The only difference between for loop and while loop is the syntax format.

 

Difference between for loop and while loop

·    In a while loop, the initialization of variable, condition applying for the processing of loop (called testing) and, incrementation of the variable, all these three steps are done in a different line.

·    In for loop, all the above three steps are done in the same line.

·    Example:: look at the red shaded part in both the below programs, you will get the difference.

 

for-loop-c
How does the for loop work?

Let us understand this with the help of an example.

for-loop-c

Let us observe the above program

  •   Step1: The compiler's control will first go to i=0, value will be assigned to the variable i=0.
  •  Step2: Then the subscript "i" will be compared with 5 if the condition satisfies, the control will go inside the for-loop body.
  •   Step3: Statements under curly{ } braces are the body of the for loop where the actual operation is performed.
  • Step4: after the execution of first iteration of the for-loop "i" will get incremented and, "i" becomes 1. Again the control will go to step2, and the same steps will be repeated till the "i" value becomes 6, and the condition becomes false. When the condition becomes false, the curser will come out of for loop.
for-loop-c

Flow chart
for-loop-c

Important points

 a) We can write i++ as i=i+1 or i+=1.

 b) It is mandatory to have two semicolons in the for-statement syntax.
 
let us see some examples

1.
#include<stdio.h>
int main()
{
 int i;
for(i=1;i<=5; )
{
  printf(%d , i);
  i++;
}
return 0;
}
Output: 1 2 3 4 5
    Here in this program, the incrementation is done inside the body, which is valid.
   Under for statement although the incrementation is done inside the body of for loop, still two semicolons are used.
   If we do not write two semi-colons, there will be an error in the program.
 
2.
#include<stdio.h>
 int main()
 {
  int i;
  i=1;
  for(;i<=5; )
   {
    printf(%d , i);
    i++;
   }
return 0;;
}

The above program is also valid, where initialization is done outside the for statement and incrementation is done under the body of for loop. 

3.
#include<stdio.h>
int main()
{
 int i;
 i=1;
 for(;i<=5;i++ )
 {
   printf(%d , i);
 }
return 0;
}

   The above program is also valid here, initialization is done out of for loop.
 
4.
#include<stdio.h> 
int main()
{
  int i;
 for(i=1;i<=5;i++);
 {
   printf(%d , i);
 }
return 0;
}
Output: 6

In the above program, as you can see, there is a semicolon after for statement. 
This semicolon will act as a termination symbol.
The cursor will not jump in the body of for loop. Because of termination after the for the statement, the statements present next to the for loop inside the curly braces will not be considered as the body of for loop. 
 A for loop will increment till "i" become 6. When i is 6 the cursor will come out of for loop and a further statement will be compiled.
 Hence the value is now 6. 6 will be printed.
 
5.
#include<stdio.h>
int main()
{
 int i;
 for(i=0,j=1;i<=5 && j<=5;i++,j++ )
 {
   printf(i=%d , i);
   printf(j=%d \n, j);
 }
return 0;
}
Output: i= 0 j=1
        i= 1 j=2
        i= 2 j=3

 As u can see in the above program we have used two variables under for loop and incremented them at time.
  We have also used the && operator.
   This states that we can use more than one variable in for loop at the same time. 
  We can also use logical operators such as (&&)and/or(||).

Nested for loop

As if statements can be nested, similar way For loops can also be nested as well as while loop can be nested.

Example::


#include<stdio.h>
 int main()
   { 
     int row, column,i=0;
     for(row=1;row<=3;row++)
       {
         for(column=1;column<=3;column++)   
           {
             printf(%d , i=i+1);
           }
           printf(“\n);
       }
 return 0;
} 

output::

1 2 3
4 5 6 
7 8 9
 




c). As we can nest for loop under for loop and while loop under while loop, similarly we can also nest for loop under while loop and while loop under for loop.

  See Also:

   while loop
   do-while loop
   break statement
   continue statement