Loop Statements

The looping statements are used to execute group of statements repeatedly until some condition is satisfied. The looping structures are

  1. While structure
  2. Do...while structure
  3. For structure

While structure

This is a simple looping statement when executed, the computer first evaluates the test condition. The control is transferred to next statement if the value is false. The body of the loop is executed repeatedly until the test condition becomes false. The control is transferred to the next statement when the test condition becomes false.

while(test condition)

{

body of the loop;

}

next statement;

Example:

#include

main ()

{

int j;

j=1;

while (j<5)

{

printf(“welcome”);

j++;

}

}

Do...While structure

When this statement is executed the body of the loop is executed first. The test condition is then evaluated. The control is transferred to the next statement, if the value is false. The body of the loop is executed repeatedly until the test condition becomes false. The control is transferred to the next statement if the test condition becomes false.

The general form is

do

{

body of the loop;

}

while(test condition);

next statement;

Example:

#include ,stdio.h>

main()

{

int j;

j=1;

do

{

printf(“welcome”);

j++;

}

while(j<5)

}

For statement

If the group of statement executed for known number of times then we can use for statement. The general form is

for(control variable; test condition; increment or decrement)

{

body of the loop;

}

next statement;

example:

#include ,stdio.h>

main()

{

int j,s;

s=0;

for(j=1;j<=100;j+)

{

s=s+i;

}

printf(“s=%d”,s);

}

Loop Statements Homework Help