10.19.2011

for Loop

At this point you think, there is already while loop function then what we need one more loop function? The answer is that for is easy to use and allows us to specify three things in single line, these three things are:
  1. Setting a loop counter to an initial value
  2. Testing the loop counter to determine whether its value has reached the number of repetitions desired.
  3. Increasing or Decreasing the value of loop counter.
The general form of for statement is as under:

for(initialise counter; test counter; increment/decrement counter)
{
  do this;
  and this;
  and this;
  and that;
}

It is important to note that the initialization ,testing and incrementation part of a for loop can be replaced by any valid expression, thus the following for loops are valid:

for(i=5; i; i--)
  printf("%d",i);
for(i<=5; j=10; j=0)
  printf("%d",i);
for(i=1; i<=10; printf("%d",i++))
;
for(scanf("%d",&i); i<=5; i++)
  printf("%d",i);
Demonstration of for loop


/*demonstration for loop program*/
/*program to print cprogrammingcodes in five times*/
#include<stdio.h>
#include<conio.h>
void main()
{
 int i;
 clrscr();
 for(i=1; i<=5; i++)
 {
   printf("cprogrammingcodes");
   printf("\n");
 }
 getch();
}

              Output of above program:
cprogrammingcodes
cprogrammingcodes
cprogrammingcodes
cprogrammingcodes
cprogrammingcodes


Versions of for loops
Now we learn how many different types for uses to print one to ten numbers.

(a) #include<stdio.h>
    #include<conio.h>
    void main()
    {
     int i;
     clrscr();
     for(i=1; i<=10; i++)
       printf("%d\n",i);
    }

(b) #include<stdio.h>
    #include<conio.h>
    void main()
    {
     int i;
     clrscr();
     for(i=1; i<=10;)
      printf("%d\n",i);
      i++;
    }

(c) #include<stdio.h>
    #include<conio.h>
    void main()
    {
     int i;
     clrscr();
     i=1;
     for(; i<=10; i++)
      printf("%d\n",i);
    }

(d) #include<stdio.h>
    #include<conio.h>
    void main()
    {
     int i;
     clrscr();
     i=1;
     for(; i<=10 ;)
      printf("%d\n",i);
      i++;
    }
(e) #include<stdio.h>
    #include<conio.h>
    void main()
    {
     int i;
     clrscr();
     for(i=1; i++<=10;)
      printf("%d\n",i);
    }
(f) #include<stdio.h>
    #include<conio.h>
    void main()
    {
     int i;
     clrscr();
     for(i=0; ++i<=10;)
      printf("%d\n",i);
    }

(g) #include<stdio.h>
    #include<conio.h>
    void main()
    {
     int i;
     clrscr();
     i=1
     for(; i++<=10;)
      printf("%d\n",i);
    }

Multiple Initialisations and increment/decrement in the for loop

The initialisation expression of the for loop can contain more than one statement by a comma.For example:

for(i=0, j=10; i<=j ; i++,j--)

Thus we see, that multiple statement can uses to initialisation and increment(or decrement). But notice there must be only one statement allowed in the test expression. This expression may contain several condition linked using logical operators as:
 for(i=0, j=10; i<=j || i==j ; i++,j--)

Nesting of for Loops


The way if statement can be nested, similarly whiles and fors can also be nested. Let's understand the nesting of for loops through program:

/*demonstration of nested for loop*/
#include<stdio.h>
#include<conio.h>
void main()
{
 int r,c,s;
 clrscr();
 for(r=1; r<=2; r++)/*outer loop*/
 {
  for(c=1; c<=2; c++) /*inner loop*/
  {
   s=r+c;
   printf("r=%d c=%d sum=%d\n",r,c,s);
  }
 }
 getch();
}
       output of above program
r=1 c=1 sum=2
r=1 c=2 sum=3
r=2 c=1 sum=3
r=2 c=2 sum=4

Thus we see in above program that, the body of outer loop is indented, and the body of the inner for loop is further indented. These multiple indentations make the program easier to understand.

1 comment:

  1. really very good concept in all program....and get lot of new thing....thnx to pyramid group

    ReplyDelete