18 Programming and Data Structures
Then the condition would be like this.
If (21)
Connect 'sales department'
Else
If (22)
Connect 'accounts section'
Else
Return to the telephone operator.
1.9 ITERATION AND RECURSION
Iteration
In a program, sometimes, it is very necessary to perform the same action for a number of times. If
the same statement is written repetitively, it will increase the program code. To avoid this problem,
iteration mechanism is applied. The statement written in iteration block is executed for a given
number of times based on certain conditions. The iteration is shown in Fig. 1.22. In C/C++ iteration
can be performed using the following statements.
for While do-zvhile
fo r (exp ression-1; exp ression-1;
express ion-1;
expression-2;
while (expression-2) do
expression-3)
{
{
statem ent;
statem ent;
statem ent;
expression-3;
}
while (expression-2) ;
expression-3;
}
The flow chart of the above iteration is as per Fig. 1.23.
Steps of 'while' loops are as follows.
1) The test condition is evaluated and if it is
true, the body of the loop is executed.
2) On execution of the body, test condition is
repetitively checked and if it is true the body
is executed.
3) The process of execution of the body will be
continued till the test condition becomes false.
4) The control is transferred out of the loop.
Fig. 1.23 Iteration
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.11.227