Decision Making and Looping
Decision Making and Looping
Decision Making and Looping
LOOPING
LOOPING
Looping method in C enable us to develop concise
programs containing repetitive processes without the
use of goto statements
In looping, the sequence of statements are executed
until some conditions for termination of the loop are
satisfied
PROGRAM LOOP
A program loop consists of two segments
Body of the loop
Control statement
PROGRAM LOOP
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
Exit-Controlled Loop
The test is performed at the end of the body of
the loop
Therefore, the body is executed unconditionally
for the first time
PROGRAM LOOP
PROGRAM LOOP
The test conditions should be carefully stated on
order to perform the desired number of loop
executions
Based on the test conditions
Control transfers out of the loop
Control sets up an infinite loop and the body is executed
over and over again
PROGRAM LOOP
The C language provides three looping
statements
The while statement
The do statement
The for statement
To calculate X
THE DO STATEMENT
On some occasions, it might be necessary to
execute the body of the loop before the test is
performed
Such situations are handled with the help of do
statement
Syntax: do
{
Body of the loop
}
while (test-condition);
The program proceeds to evaluate the body of the
loop first
At the end of the loop, the test condition in the
while statement is evaluated
THE DO STATEMENT
If the condition is true, the program continues to
evaluate the body of the loop once again
This process continues as long as the condition
is true
When the condition becomes false, the loop will
be terminated and the control goes to the
statement that appears immediately after the
while statement
The test condition is evaluated at the bottom of
the loop
Exit-controlled loop
The body of the loop is always executed at least
once
DO...WHILE PROGRAM
main()
{
int i=1
do
{
printf("\nProgram for
do...while loop");
i++;
}
while(i<=5);
}
OUTPUT:
Program for
loop
Program for
loop
Program for
loop
Program for
loop
Program for
loop
do...while
do...while
do...while
do...while
do...while
DO...WHILE PROGRAM
main()
{
int i=7
do
{
printf("\nProgram for do...while loop");
i++;
} while(i<=5);
}
OUTPUT:
Program for do...while loop
STEP 3:
COMPARISON OF THREE
LOOPS
JUMPS IN LOOPS
Sometimes, when executing a loop it
becomes desirable to skip a part of the
loop or to leave the loop as soon as a
certain condition occurs
Example: searching for a number in a list
C permits a jump from one statement to
another within a loop as well as a jump out
of a loop
goto statement
A goto statement can transfer the control to any
place in a program, it is useful to provide
branching within a loop
Important use of goto is to exit from a deeply
nested loops when an error occurs
AVOIDING GOTO
Reasons to avoid goto
When goto is used, many compilers generate a
less efficient code
Using many goto makes a program logic
complicated and renders the program
unreadable
Example:
if(m%5 == 0 && n%5 ==0) is same as if(! (m%5) && ! (n
%5))
SELECTING A LOOP
Analyze the problem and see whether it required a
pre-test or post-test loop
If it requires a post-test loop, then use do while
If it requires a pre-test loop, then can use for or
while
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
Note that both the loops can be implemented by all
the three control structures
THE END