1) The document discusses different types of loops in programming including pretest loops, post-test loops, counter controlled loops, and event controlled loops. It explains the concepts of loop initialization and updating.
2) Specific loop structures are discussed for the C programming language, including the while, do-while, and for loops. Examples of each loop type are provided.
3) Loops are fundamental programming constructs that allow code to be repeated until a condition is met. This document covers the core concepts of loops including initialization, testing conditions, and updating values to control loop execution.
1. B Y
Y N D ARAVIND
A S S O C I A T E P R O F E S S O R , D E P T O F C S E
N E W T O N ’ S G R O U P O F I N S T I T U T I O N S , M A C H E R L A
@2020 Presented By Mr.Y N D Aravind
1
Repetation
2. P A R T - I I
CONCEPT OF LOOP
PRETEST AND POST-TEST LOOPS
INITIALIZATION AND UPDATING
EVENT AND COUNTER CONTROLLED LOOPS
LOOPS IN C
OTHER STATEMENTS RELATED TO LOOPING
LOOPING APPLICATIONS
@2020 Presented By Mr.Y N D Aravind
2
SELECTION – MAKING DECISIONS
3. Concept of a loop
Presented By Mr.Y N D Aravind
3
The below diagram shows the concept of loop. In this diagram, the loop is
repeated again and again it will never stop.
But we want the loop to be stopped after our work is over. To do this we
need to put a condition to control the loop. This means that we have to
design the loop so that after each iteration the condition must be checked.
On checking the condition if it is true we repeat the loop again or if it is
false we terminate the loop. This test condition is called loop control
expression and the variable in the test expression that is used to control the
loop is called loop control variable.
An action of a
series of actions
4. Pretest and Post-test Loops
Presented By Mr.Y N D Aravind
4
Suppose if we need to test the end of the loop then where we have to test.
The answer to this question is before the iteration or after the iteration of
the loop. We can have both. They are
Pretest loop or entrance controlled loop or entry controlled loop
Post-test loop or exit controlled loop
The diagrammatic representations for the pretest and post-test loops are as follows.
False
True
True
False
condition
condition
An action or series of
acttion
An action or series of
actions
5. Loop Initialization and Loop Update
Presented By Mr.Y N D Aravind
5
Before we start the loop, we require some preparation. This
preparation is called loop initialization. Initialization must be
done before we execute the body of the loop.
The initialization is shown as a process box. The initialization can
be either implicit or explicit. Explicit initialization include direct
code at the beginning of the loop where as implicit initialization has
no direct code.
How can the condition that controls the loop can be true for a
while and then become false. The answer to the question is
something must happen inside the body of the loop to change the
condition. Otherwise we will have infinite loop. The actions that
cause that change in the loop is called loop update.
6. Entry Control and Exit Contro Loops
.
False
True
True
False
Presented By Mr.Y N D Aravind
6
Initilization
Actions, Update actions
Test
Initilization
Actions, Update actions
Test
7. 1. Counter control loops
When we know the number of times the loop is to be
repeated then we call it as counter controlled loop.
Counter Controlled Loops and Event Controlled Loops
Presented By Mr.Y N D Aravind
7
Pretest Loops
False
True
Post – test Loops
Set count to 0
Actions, Update Condition
Count < 4
True
Set count to 0
Actions, Update Condition
Count < 4
False.
8. 1. Event control loops
In an event controlled loop, an event changes the
control expression from true to false
Counter Controlled Loops and Event Controlled Loops
Presented By Mr.Y N D Aravind
8
Pretest Loops
False
True
Post – test Loops
Initialization
Actions, Update actions
condition
True
Initilization
Actions, Update actions
condition
False.
9. Loops in C
In C, there are three loop statements:
while
do-while
for
The first and third are pretest loops and second is post test loop. We can use all of
them for event and counter controlled loops. The while and do-while can be
used for event controlled loops and for is used for counter controlled loop.
Presented By Mr.Y N D Aravind
9
10. While Loop
Syntax :-
while (expression)
{
Body of Loop;
}
Statement - x ;
LOOPS
Presented By Mr.Y N D Aravind
10
This type of loop is also called an entry controlled loop construct. It is also called as pretest
loop. As it is pretest loop, it will test the expression before every iteration of the loop. The expression
is executed and if is true then the body of the loop is executed this process is repeated until the
boolean expression becomes false. Ones it becomes false the control is a transferred out of the
loop.
Expression
Body of the loop
Next Stmt
Flow Chart
False
True
11. Example Program
i = 1;
while(i <= 5))
{
Printf (“ i = %d n”, i);
i++;
}
#include<stdio.h>
void main()
{
int n, i=1,c=0;
printf(“enter the value of n”);
scanf(“%d”, &n);
while(i<=n)
{
if(n%i==0)
c++;
i++;
}
if(c==2)
printf(“ the given number is prime %d”, n);
else
printf(“ the given number is not prime %d”, n);
}
While Loop
Presented By Mr.Y N D Aravind
11
12. do - While Loop
Syntax :-
do
{
Body of Loop;
} While (expression);
Statement - x ;
LOOPS
Presented By Mr.Y N D Aravind
12
This type of loop is also called an exit controlled loop statement. i.e., the
Boolean expression evaluated at the bottom of the loop and if it is true then body of
the loop is executed again and again until the Boolean expression becomes false.
Ones it becomes false the control is transferred out of the loop executing the next
statement.
Expression
Body of the loop
Next Stmt
True
False
13. Example Program
i = 1;
do
{
Printf (“ i = %d n”, i);
i++;
}
while(i <= 5) ;
#include<stdio.h>
Void main()
{
int n, r,rev=0;
printf(“enter the value of n”);
scanf(“%d”, &n);
do
{
r=n%10;
rev=rev*10+r;
n=n/10;
}while(n>0);
printf(“ the reverse of the given number is %d”, rev);
}
do - While Loop
Presented By Mr.Y N D Aravind
13
14. for loop
Syntax :-
for( initilization; cond expression; increment)
{
Body of Loop;
}
Statement - x ;
LOOPS
Presented By Mr.Y N D Aravind
14
Expression
Initilization
Next Stmt
True
False
Stmt
Control Variable
15. Example Program
for( i=0; i<= 10; i++)
{
Printf (“ i = %d n”, i);
}
#include<stdio.h>
Void main()
{
int n, i, sum=0;
printf(“enter the value of n”);
scanf(“%d”, &n);
for(i=1; i<=n; i++)
{ if(n % i == 0)
{ sum=sum+i; }
}
if(sum == n)
printf(“ Perfect Number”);
else
printf(“ Not Perfect Number”);
}
For Loop
Presented By Mr.Y N D Aravind
15
16. Y. N. D. ARAVIND
ASSOCIATE PROFESSOR, DEPT OF CSE
N E W T O N ’ S G R O U P O F I N S T I T U T I O N S , M A C H E R L A
Presented By Mr.Y N D Aravind
16
Thank YouThank You