Ch14
Ch14
Ch14
In a C program there may be situations where we may want to execute same block of statements many
times.
◦ All programming languages offers loop control structure (also known as looping) allowing
programmers to execute a statement or group of statements multiple times.
▪ Here control structure mean that the flow of execution may not be sequential and control may
be transferred to any statement based on given conditions.
In looping, the sequences of statements are executed until some exit condition is satisfied.
The looping construct is composed of two parts:
▪ body of loop and
▪ control statement.
Depending on the place of control statement in loop, it can be classified as entry controlled and exit
controlled loop.
In entry controlled loop the exit/control condition is checked before executions of statements inside
loop body
In exit controlled loop the exit/control condition is checked after executions of loop body.
◦ This means that in case of exit controlled loop, body will be executed at least once before exiting
from the loop.
Note: that as we are checking condition at the entry point, it is known as entry-controlled loop
Nested Loops
Nesting of different types of loops is possible by using one loop control structure within another,
irrespective of the type of loop control structures used.
For example, in for loop, while or do while loop can be used.
Selecting a Loop
To select the more appropriate loop for our program, we may use the following steps:
For a given problem, identify whether entry-controlled or exit-controlled loop is required
For entry controlled loop, we may use for or while loop construct. Use of for loop is advisable for
counter based exit condition.
For exit controlled loop, we have choice of do..while construct.
Also while using any loops in our program following three important points are to be considered
carefully to avoid miscellaneous behavior of a loop:
1. break statement
2. continue statement