for Loop

  • It works as while loop, But Syntax is very good as compare to other two loops.
  • Condition is checked before entering in the body of loop.
  • You can also call this as ,entry control loop.
  • Many programmer Prefer to go with is loop , because In this loop
    initialization,  condition, a nd increment of the variable is done in an single line, Because of that You can Understand Concept of loop in better way.
  • Reading ability is very good as compare to other two loop.

Syntax

for(initializing ; condition ; increment)
{

//body of for loop.
}

Lets see the same program of printing number from 1 to 5 ,with for loop.

#include<stdio.h>       //header file
 void main()
{
int i;

for(i = 1; i <= 5 ; i++)
{
// body of for loop.
printf("%d\n",i);      //Printing the value of  i , for every iteration. 

}
}
        

Output:

1
2
3
4
5
  • Step by Step Explanation.
  • Valid values of i = 1,2,3,4,5 , for this program.
  • For i = 1,First Condition got checked and it was evaluated to true, so it print the value of i. which is 1 and incremented it by 1.
  • Now value of has become 2,condition got check it evaluated to true and it printed the value of i which is 2 and incremented it by 1.
  • Now i=3,Again with i =3,condition got check it evaluated to true and it printed the value of i which is 3 and incremented it by 1.
  • Above step , will repeat until the condition gets evaluated to false.
  • In this example,  for  value of i = 6 ,loop will get terminate(stop) and control will go to the next piece of code.
  • Control will not stay within the for loop, it will go to the next statements of the code.







Further Concepts





Practice Programs


  • Programs Regarding If-else Statements
  • Program Regarding Nested If - statements
  • Programs Regarding loops


 Further Topics