Saturday, May 10, 2008

Repitition & loop control statements

This type of statements helps the computer to execute a
group of statements repeatedly.

This allows a set of instruction to be performed until a
certain condition is reached.

There are three types of loops in C:

1. for loop
2. while loop
3. do-while loop

The for loop

There are three parts of for loop:

a)counter initialization.
b)check condition
c)modification of counter.

Syntax:
for (variable initialize; check condition; modify counter)

{
statements 1;
-----------;
-----------;
statements n;
}

Explanation:

1. The initialization is usually an assignment that is used
to set the loop control variable.

2. The condition is a relational expression that determines
when the loop will exit.

3. The modify counter defines how loop control variables will
change each time the loop is repeated.

These three sections are separated by semicolon (;).

The for loop is executed as long as the condition is true. When,
the condition becomes false the programe execution will
resume on the statement following the block.

Advantage of for loop over other loops:

All three parts of for loop (i.e. counter initialization,
check condition, modification of counter) are implemented
on a single line.

A structure image of for loop >


An example program to print a message 5 times using for loop:



output:

Repitition & loop control statements


Explanation of the program:

The o/p will be in the loop 1 time, 2times till 5 times.

Something more about for loop:

1. for (p=1,n=2;n<17;n++):- we can assign multiple variable
together in for loop.

2. for (n=1,m=50;n<=m;n=n+1,m=m-1):-The increment section
may also have more than one part as given.

3. for (i=1,sum=0;i<20&&sum<100;++i):-The test condition
may have any compound relation as given.

4. for (x=(m+n)/2;x>0;x=x/2):-It is also permissible to use
expressions in the assignment statements of initialization
and increment section as given.

5. for (;m!=100;):-we can omitted the initialization and
increment section to set up time delay.

while loop

It is a primitive type looping control because it repeats
the loop a fixed no. of time. It is also called entry
controlled loop statements.

Syntax:

while (test_condition)
{
body of loop
}

Explanation:

The test condition is evaluated if the condition is true,
the body of loop will be executed.

A structure image of while >




An example of program to print the number 1 to 10 using
while loop:



output:




The do-while loop

The minor Difference between the working of while and do-while
loop is the place where the condition is tested.

The while tests the condition before executing any of the
statements within the while loop

As against this, the do-while loop tests the condition after
having executed the statement within the loop.

syntax:

do
{
body of loop;
}
while (test condition);

A structure image of do-while loop >




Example of program using do-while loop:




output:



Explanation of the program:

First it will print "hello there" then it will go for
condition statements.




0 comments:

Copyright

Copyright © 2008 C Tutorial, All rights are reserved