Sep 26

Nested loop is one loop inside another loop.
Example

#include<stdio.h>
main()
{
int i,j;
for(i=1;i<=4;++i)
{
printf(”%d\n”,i);
for(j=1;j<=i;++j)
printf(”%d “,j);
}
}
Output
1
1 2
1 2 3
1 2 3 4

here,an inner for loop is written inside the outer for loop. For every value of i, j takes the value from 1 to i and then value of i is incremented and next iteration of outer loop starts ranging j value from 1 to i.

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Blogplay
  • LinkedIn
  • MySpace
  • Reddit
  • RSS
  • Twitter

written by Shweta

Sep 18

The for allows us to specify three things about a  loop in single line :

  1. Setting a loop counter to an initial value.
  2. Test conditions.
  3. Increasing the value of loop counter each time the program segment within the loop has been executed.

Syntax

for (initialize counter ; test counter; increment counter)

{

}

Concept of execution of For loop

1. Initial condition
2. testing condition
if true (step 3a) and   if false (step 3b)
3a. body of the loop 3b . Stop (out of the loop)
4. counter
5. goto step 2 and run till the test condition is true.
  • Initialization,testing and incrementation part of for loop can be replaced by any valid expression.

Example

  • for (i=10; i ; i–)

         printf(”%d”,i);

  • for (i<4; j=5; j=0)

         printf(”%d”,i);

  • for (i=1; i<=10;printf(”%d”,i++))

         ;

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Blogplay
  • LinkedIn
  • MySpace
  • Reddit
  • RSS
  • Twitter

written by Shweta

Aug 22

Syntax:

do

{

.

.

.

}while(this condition is true);

The only difference between while and do-while is that do-while would execute its statements at least once,even if the condition fails for the first time.

Example

main()

{

do

{

printf(”Hello”);

}while(4<1)

}

printf  would be executed once, since first  the body of the loop is executed and then condition is tested.

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Blogplay
  • LinkedIn
  • MySpace
  • Reddit
  • RSS
  • Twitter

written by Shweta