Ch14

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Chapter 14

Loop Control Structure

 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.

Three basic loop control structure provided by C language:


1. for
2. while
3. do...while

The For Loop:-


 The for loop is normally used when we want to execute block of statements fixed number of times.
 To make for loop more dynamic, we can use the exit condition inside for loop.

Syntax of For Loop:-


for (expression1; expression2; expression3)
{
Statement-block;
}
 The header of for loop contains three expressions separated by semicolons in parenthesis.
 All these expressions are optional.
 Statement-block which is also known as body of for loop may contain simple statement or compound
statement.
Note:
expressionl works for initializing value of counter variable that is used to control the number of times a
loop is to be executed. This variable is known as control variable.
expression2 works as test condition. A control variable is used for checking loop terminating enteria.
expression3 works for incrementing or decrementing value of control variable.

Nested for loop:-


 Nested for loop means using one for loop within another for loop.

Comma Operator in for Loop:-


 In for loop we can initialize more than one parameters using comma operator.
 Similarly we may use comma operator to increment or decrement various variables in a for loop.

The While Loop:-


 When number of iteration cannot be pre-determined and when loop terminating condition is to be
tested before entering the loop, at that time use of while loop is more suitable.

Syntax of While Loop:-


while (test expression)
{
statement-block; /* body of while loop */
}

 it evaluates the test expression first.


◦ If test expression is evaluated to true, then body of loop containing statement-block is executed.
▪ Again program evaluates the test Expression. This process is repeated until the test expression is
evaluated to false.
▪ When test expression is evaluated to false then loop is terminated and control is passed to the
next statement after the loop in a program.
◦ If test expression is evaluated to false, the body part will not be executed at all.
 Body of loop may contain simple or compound statement

Note: that as we are checking condition at the entry point, it is known as entry-controlled loop

The do-while loop:-


 The do...while loop should be used when the test expression is to be checked after executing body of
loop. As we are checking test condition at the end of loop, the do...while loop is a kind of exit-
controlled loop.
 Note that in do... while loop, body of loop will be executed at least once.

Syntax of do...while loop:-


do
{
statement-block; /* body of loop */
}
while( test expression);

 Thể bódy of loop containing statement-block is executed first.


 Then it evaluates the test expression.
◦ If the test expression is evaluated to true then body of loop containing statement-block is executed
again.
 After executing statement-block, once again text expression is evaluated.
◦ This process is repeated until the test expression is evaluated to false.
 When test expression is evaluated to false then loop is terminated and control is passed to the next
statement after the loop in a program.
 Body of loop may contain simple or compound statement.

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:

 Initialization of loop counter


 To test exit condition to come out from the loop
 Incrementing or decrementing loop counter

Skipping a Part of Loop


During execution of a program, sometimes we may want to skip some of the statements or
iterations of the loop. This is possible in C language by using following statements:

1. break statement
2. continue statement

The break Statement


 Sometimes during execution of any loop, we may want to terminate loop instantly without even
execution of other statements of loop. This is possible using the break statement.
 When the break statement is encountered inside the loop, the loop is immediately terminated and
program control executes the next statement following the loop.
 The break statement can also be used to terminate a case in the switch statement.

The continue Statement


 The continue statement works to some extent like the break statement. Instead of forcing termination of
loop.
 However, continue statement forces the next iteration of the loop to take place, skipping any code in
between.

The Infinite Loop


1. Any loop in a program becomes infinite loop if it runs forever and program control never comes out of
it.
2. The loop becomes infinite due to non availability of exit condition in the logic of loop.

!! Vision Computer Education !!


Query to call:
satishsir.vision@gmail.com
Whatsup : +91-9409209969

You might also like