2.1
LOOPING
STATEMENTS
When
one or more statement are to be executed repeatedly either a specified number
of times or until a particular condition is being satisfied, loop is used.
There are three types of loops in C.
·
for
·
while
·
do … while
Ø
Looping is also called repetition or
iteration.
Ø Looping
can also be implemented with the help of goto statement but the goto statement
is avoided in structured programming because it destroys the concept of
“locality of references.” Locality of references increases the efficiency of
memory access.
1.
for – loop:
The for loop consist of two parts.
·
Header
·
body – C statements that are to be executed
repeatedly.
The header contains the three section:
(a)
Initialization
(b)
Condition
(c)
Statement
·
It takes the following form:
·
generally we use the initialization in place
of s1 and increment/decrement in place of s2, c is the test condition.
·
If body have more than one statement then
braces are necessary.
·
The initialization statement s1 is not
executed repeatedly; it is executed only once, just before entering the loop.
·
The statements to be executed repeatedly are
s3 and s2, till the condition c remains true.
·
The statements inside the header is executed
in such a way as if it is the last statement of body of the for loop
·
Any section of the header of for loop can be
left blank. If condition is left blank, it is considered as true for ever. So
it will be an infinite loop, which is the loop that never terminate.
The statement s1 and s2 will be
repeated forever.
·
The for loop must be documented specifying its
purpose.
Error
in Loop – The errors in loops are caused due to
incorrect conditions. There may be three types of logical error in loops:
(a) Infinite looping: it results when
the loop condition never become false.
(b) Boundary case problem: The
condition is such that it repeats the loop either one time more or one time
less than required.
(c) The condition may be such that it
is never true. In this case the loop statements will not be executed even for a
single time.
Nesting
of Loop – The
statements in the body of for loop can be any valid C statement. So it can be
another for loop. It is called nesting of for loop. In case of nesting, one
loop must be completely inside the other loop. The overlapping of two loops is
not possible.
/* start of outer loop */
/* start of inner loop */
…..
/* end of inner loop */
……
/* end of outer loop */
Break statement – Break is used at two places:
● inside switch --- case statement
● inside the loop
As soon as break statement is
encountered, the execution comes out of the structure immediately. If break is
inside the loop, the loop is terminated.
● The break statement inside the loop
is only meaningful when it is provided with if statement.
Continue statement – It is used in loops. As soon as it is executed the statement physically appearing after continue statement are skipped and loop continues. It is meaningful to take continue inside if statement.
2. while – Loop: If
we want to do something a fixed number of times, we use while loop statement
which takes following form:
initialize loop counter;
while (condition)
{
Statements;
}
·
The while is an entry controlled loop
statement.
·
The test – condition is evaluated and if the
condition is true the body of loop is executed. After execution of the body the
test condition in once checked if it is
true the body of while loop is evaluated otherwise execution exit from the
loop. If at the entry time if test condition is false then body of loop would
not executed at all.
·
The body of while loop may have one or more
statements. The braces are needed only if the body contain two or more
statements.
3. The do…while Loop – There is minor difference between
while and do-while.
This difference is the place where the
condition is tested. Do-while is the exit-controlled loop that is why the body
of the loop is always executed at least once whether the test condition is true
of false. It takes the following form:
do
{
do_work();
}
while (condition);
is equivalent to
do_work();
while
(condition)
{
do_work();
}
that (as long as the continue statement is not
used) is technically equivalent to the following (though these examples are not
typical or modern style):
while
(true)
{
do_work();
if (!condition) break;
}
or
LOOPSTART:
do_work();
if (condition)
goto LOOPSTART;
·
The while is an entry controlled loop
statement. The test – condition is evaluated and if the condition is true the
body of loop is executed. After execution of the body the test condition in
once checked if it is true the body of while loop is evaluated otherwise
execution exit from the loop. If at the entry time if test condition is false
then body of loop would not executed at all.
·
The body of while loop may have one or more
statements. The braces are needed only if the body contains two or more statements.
Examples:
int counter = 5;
int factorial = 1;
do {
factorial *= counter--; /* Multiply, then decrement. */
} while (counter > 0);
printf("factorial of 5 is %d\n", factorial);
Try following
example to understand do...while loop. You can put the following code
into a test.c file and then compile it and then run it.
#include
<stdio.h>
main()
{
int i = 10;
do{
printf("Hello %d\n", i );
i = i -1;
}while ( i > 0 );
}
|
This will produce
following output:
Hello 10
Hello 9
Hello 8
Hello 7
Hello 6
Hello 5
Hello 4
Hello 3
Hello 2
Hello 1
|
You can make use
of break to come out of do...while loop at any time.
#include
<stdio.h>
main()
{
int i = 10;
do{
printf("Hello %d\n", i );
i = i -1;
if( i == 6 )
{
break;
}
}while ( i > 0 );
}
|
This will produce
following output:
Hello 10
Hello 9
Hello 8
Hello 7
Hello 6
|
No comments:
Post a Comment