Decision Making and Looping

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 31
At a glance
Powered by AI
The key takeaways are that there are different types of loops in C like while, do-while and for loops. Loops are used to repeatedly execute a block of code and consist of initialization, condition checking, incrementing/decrementing and body. Statements like break, continue and goto can change the flow within loops.

The different types of loops in C are while loop, do-while loop and for loop.

The different parts of a loop are initialization, condition checking, incrementing/decrementing and the body of the loop.

DECISION MAKING AND

LOOPING

DECISION MAKING AND


LOOPING
INTRODUCTION
It is possible to execute a segment of a program
repeatedly by introducing a counter and later testing
it using the if statement
We need to initialize and increment a counter and test
its value at an appropriate place in the program for
the completion of the loop

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

The control statements tests certain


conditions and then directs the repeated
execution of the statements contained in
the body of the loop
Depending on the position of the control
statement in the loop, a control structure
may be classified either as
The entry-controlled loop
The exit-controlled loop

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

A looping process, include the following four steps

Setting and initialization of a counter


Execution of the statements in the loop
Test for a specified condition for execution of the loop
Incrementing the counter

The test determine whether the loop has been


repeated the specified number of times or to
determine whether a particular condition has been
met

PROGRAM LOOP
The C language provides three looping
statements
The while statement
The do statement
The for statement

THE WHILE STATEMENT


Simplest looping structure
Entry-controlled loop statement
Syntax:
while (test condition)
{
Body of the loop
}

The test-condition is evaluated and if the


condition is true, then the body of the loop is
executed
After execution of the body, the test
condition is once again evaluated and if it is
true, the body is executed once again

THE WHILE STATEMENT


This process of repeated execution of the
body continues until the test condition
finally becomes false and the control is
transferred out of the loop
On exit, the program continues with the
statement immediately after the body of the
loop
The body may have one or more statements
The braces are needed only if the body
contains two or more statements
It is a good practice to use braces even if
the body have one statement

THE WHILE 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

THE FOR STATEMENT


Entry-controlled loop
Syntax:
for (initialization; test-condition; increment)
{
body of the loop
}

The execution of the for statement is as


follows:
STEP 1:
Initialization of the control variables is done first, using
assignment statements. Example: i = 1 and count = 0
The variables used are known as loop-control variables

THE FOR STATEMENT


STEP 2:

The value of the control variables is tested using the


test-condition.
The test condition is a relational expression that
determines when the loop will exit. Example: i < 10
If the condition is true, the body of the loop is
executed; otherwise the loop is terminated
the execution continues with the statement that
immediately follows the loop

STEP 3:

When the body of the loop is executed, the control is


transferred back to the for statement after evaluating
the last statement in the loop.
The control variable is incremented using an
assignment statement and the new value of the
control variable is again tested to see whether it
satisfies the loop condition. Example: i = i + 1
The process continues till the value of the control
variable fails to satisfy the test condition

THE FOR STATEMENT


int count;
for (count = 1 ; count <= 10 ; +
+count)
printf ("\n%d", count);

THE FOR STATEMENT


Features of for loop
for(int i = 1, j = 2 ; i<=5 ; i++, j = j+2)
printf("\n %d", i*j);

More than one variable can be initialized at a time


The increment section may also have more than
one part
The test-condition may have any compound
relation and the testing need not be limited only
to the loop control variables
for(int i = 1 ; i <= count && sum < 10; i++)
sum += i;

Unique aspect: one or more sections of for


statement can be omitted
for( ;; )
statement;

THE FOR STATEMENT


Nesting of for loops
One for statement within another for statement
for (i=1;i<10;++i)
{
.
for (j=1;j!=5;++j)
{
.
}
.
}

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

JUMPING OUT OF A LOOP


Early exit from a loop can be accomplished
by using the break statement or the goto
statement
break used in switch statement and goto
used in ifelse construct
These can be used in while, do, or for loop

JUMPING OUT OF A LOOP


break statement
When break statement is encountered inside a
loop, the loop is immediately exited and the
program continues with the statement
immediately following the loop
When the loops are nested , the break would exit
from the loop containing it
The break will exit only single 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

SKIPPING A PART OF A LOOP


Continue statement
Unlike the break which causes the loop to be
terminated, the continue causes the loop to be
continued with the next iteration after skipping
any statements in between
SKIP THE FOLLOWING AND CONTINUE WITH
THE NEXT ITERATION

The format is:


continue;

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

CONCISE TEST EXPRESSION


Test expressions used in if, for, while and do
statements that are evaluated and compared
with zero for making decisions
Every integer expression has a true/false value,
we need not make explicit comparisons with zero
The expression x is true whenever x is not zero,
and false when x is zero
Applying ! Operator, we can write concise test
expressions without using any relational
operators
if( expression == 0 ) is equivalent to if(! expression)
if( expression != 0 ) is equivalent to if( expression)

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

You might also like