Iterative Statements

PropellerAds

while statement

The while statement is used when the program needs to perform repetitive tasks. The general form of a while statement is:

while ( condition)
statement ;

The program will repeatedly execute the statement inside the while until the condition
becomes false(0). (If the condition is initially false, the statement will not be executed.)
Consider the following program:

main( )
{ 
int p, n, count;
float r, si;
count = 1;
while ( count <= 3 )
{
printf ( "\nEnter values of p, n and r " ) ;
scanf(“%d %d %f", &p, &n, &r ) ;
si=p * n * r / 100 ;
printf ( "Simple interest = Rs. %f", si ) ;
count = count+1;
}
}
Some outputs of this program:
Enter values of p, n and r 1000 5 13.5
Simple Interest = Rs. 675.000000
Enter values of p, n and r 2000 5 13.5
Simple Interest = Rs. 1350.000000
Enter values of p, n and r 3500 5 13.5
Simple Interest = Rs. 612.000000

The program executes all statements after the while 3 times. These statements form what
is called the ‘body’ of the while loop. The parentheses after the while contain a condition.
As long as this condition remains true all statements within the body of the while loop
keep getting executed repeatedly.

do-while Loop

The body of the do-while executes at least once. The do-while structure is similar to the while loop except the relational test occurs at the bottom (rather than top) of the loop. This ensures that the body of the loop executes at least once. The do-while tests for a positive relational test; that is, as long as the test is True, the body of the loop continues to execute. The format of the do-while is

do
{ 
block of one or more C statements; 
}
while (test expression)

The test expression must be enclosed within parentheses, just as it does with a while
statement.
for Loop
The for is the most popular looping instruction. The general form of for statement is as
under:

for ( initialise counter ; test counter ; Updating counter )
{
do this;
and this;
and this;
}

The for allows us to specify three things about a loop in a single line:
(a) Setting a loop counter to an initial value.
(b) Testing the loop counter to determine whether its value has reached the number of
repetitions desired.
(c) Updating the value of loop counter either increment or decrement.

NESTING OF LOOPS

C programming language allows using one loop inside another loop. Following section shows few examples to illustrate the concept.

Syntax: The syntax for a nested for loop statement in C is as follows:

for ( init; condition; increment )
{
for ( init; condition; increment)
{
statement(s);
}
statement(s);
}
The syntax for a nested while loop statement in C programming language is as follows:
while(condition)
{
while(condition)
{
statement(s);
}
statement(s);
}
The syntax for a nested do...while loop statement in C programming language is as follows:
do
{
statement(s);
do
{
statement(s);
}while( condition );
}while( condition );

A final note on loop nesting is that you can put any type of loop inside of any other type of loop.
For example, a for loop can be inside a while loop or vice versa.

Try Now – Programming In C MCQs