3. 4.3 Iterative Statements
3/8/2016
3
Looping
In looping, a sequence of statements are
executed until some condition is satisfied
which is placed for termination of the loop
A program loop consists of two segments
a. body of the loop
b. the control statement
The control is tested always for execution of
the body of the loop
6. Entry controlled loop
The control conditions
are tested before the
start of the loop
execution.
If the conditions are
not satisfied , then the
body of the loop will
not be executed.
Entry controlled loop
is also known as pre-
test loop.
Example: – While
Loop
for Loop 3/8/2016
6
7. Exit Controlled loop
he Test is performed
at the end of the body
of the loop and there
fore the body is
executed
unconditionally for the
first time.
Exit controlled loop is
also known as post-
test loop.
Ex- ->Do-While
3/8/2016
7
10. while loop
3/8/2016
10
Example
1.Program to print first 10 numbers
2. Program to add first 10 numbers
3. Program to add first 10 even numbers
4. Program to add first 10 odd numbers
12. for loop
3/8/2016
12
Examples
1.WAP to accept number and check whether it is
prime or NOT.
2. WAP to accept number and check whether it
is Armstrong or NOT.
17. Sentinel loops
3/8/2016
17
Loops may be classified into two categories
1. Counter controlled loops
2. Sentinel controlled loops
1. Counter controlled loops -
- we know in advance how many times the loop
will be executed.
- use control variable known as counter.
- counter must be initialized, tested and updated
properly
- also known as repetition loop
18. 3/8/2016
18
2. Sentinel controlled loop
- a special value is used to change the loop
control expression from true to false.
- the control variable is called as sentinel
variable.
- also called as indefinite repetition loop
because of number of repetition loop because
the number of repetitions is not known before
the loop begins executing
19. Selecting a loop
3/8/2016
19
Analyze the problem and see whether it required
a pre – test or post – test.
If it requires a post – test loop, then we can use
only one loop, do while
If it requires a pre – test loop, then we have two
choices : for and while loop
Decide whether the loop termination requires
counter based control or sentinel – based control.
Use for loop if the counter based control is
necessary
Use while loop if the sentinel based control is
required.
20. Jumps in loop
3/8/2016
20
Sometimes when executing loop it becomes
desirable to skip a part of the loop or to leave
the loop as soon as certain condition occurs.
C permits jumps from one statement to
another within a loop as well as a jump out of
loop.
21. Jumping out of loop
3/8/2016
21
while (------)
{
-----
----
if (condition)
break;
-----
-----
}
-----
Exit
Fro
m
loop
25. The goto statement
3/8/2016
25
To branch unconditionally from one point to
another in the program
goto requires a label to identify the palce
where the branch is to be made.
label is variable name followed by colon
label is placed immediately before the
statement where the control is to be
transferred .
28. Jumping within and exiting from the
loops with goto statement
while(--------)
{
if(error)
goto stop;
-------
if (condition)
goto abc;
-------
-------
abc:
}
Stop:
------
------
for(-------)
{
------
for( -------)
{
------
if(error)
goto error;
------
}
-------
}
error:
---------
--------
3/8/2016
28
Exit
Fro
m
loop
Jump
Within
loop
Exit
Fro
m
two
loop
s
29. 3/8/2016
29
Note –
Avoid goto – because
1. Many compilers may generate less efficient
code.
2. Makes program logic complecated
3. Renders program unreadable