SlideShare a Scribd company logo
1 of 16
Download to read offline
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
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
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
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
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.
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
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.
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.
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
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
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
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
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
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
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
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

More Related Content

What's hot (18)

Variable hoisting in JavaScript
Variable hoisting in JavaScriptVariable hoisting in JavaScript
Variable hoisting in JavaScript
 
Looping
LoopingLooping
Looping
 
Loops in C
Loops in CLoops in C
Loops in C
 
Looping in c++
Looping in c++Looping in c++
Looping in c++
 
Loops c++
Loops c++Loops c++
Loops c++
 
Loops in c
Loops in cLoops in c
Loops in c
 
Looping in c++
Looping in c++Looping in c++
Looping in c++
 
Looping statements in Java
Looping statements in JavaLooping statements in Java
Looping statements in Java
 
Computer
ComputerComputer
Computer
 
Stack and queue -polish notations
Stack and queue -polish notationsStack and queue -polish notations
Stack and queue -polish notations
 
Loops in c
Loops in cLoops in c
Loops in c
 
Comp ppt (1)
Comp ppt (1)Comp ppt (1)
Comp ppt (1)
 
Loops in c language
Loops in c languageLoops in c language
Loops in c language
 
Flow of control ppt
Flow of control pptFlow of control ppt
Flow of control ppt
 
Control statements
Control statementsControl statements
Control statements
 
Loops in C Programming Language
Loops in C Programming LanguageLoops in C Programming Language
Loops in C Programming Language
 
C Language - Switch and For Loop
C Language - Switch and For LoopC Language - Switch and For Loop
C Language - Switch and For Loop
 
Control statements in c
Control statements in cControl statements in c
Control statements in c
 

Similar to Repetations in C (20)

C Programming - Decision making, Looping
C  Programming - Decision making, LoopingC  Programming - Decision making, Looping
C Programming - Decision making, Looping
 
itretion.docx
itretion.docxitretion.docx
itretion.docx
 
Loops In C++
Loops In C++Loops In C++
Loops In C++
 
Loops
LoopsLoops
Loops
 
Decision making and looping
Decision making and loopingDecision making and looping
Decision making and looping
 
Fundamentals of Programming Chapter 7
Fundamentals of Programming Chapter 7Fundamentals of Programming Chapter 7
Fundamentals of Programming Chapter 7
 
Loop and while Loop
Loop and while LoopLoop and while Loop
Loop and while Loop
 
loops and iteration.docx
loops and iteration.docxloops and iteration.docx
loops and iteration.docx
 
Loops in c
Loops in cLoops in c
Loops in c
 
loops in C ppt.pdf
loops in C ppt.pdfloops in C ppt.pdf
loops in C ppt.pdf
 
Unit II chapter 4 Loops in C
Unit II chapter 4 Loops in CUnit II chapter 4 Loops in C
Unit II chapter 4 Loops in C
 
C++ unit-1-part-14
C++ unit-1-part-14C++ unit-1-part-14
C++ unit-1-part-14
 
Loops
LoopsLoops
Loops
 
M C6java6
M C6java6M C6java6
M C6java6
 
Programming Fundamentals lecture 8
Programming Fundamentals lecture 8Programming Fundamentals lecture 8
Programming Fundamentals lecture 8
 
UNIT 2 PPT.pdf
UNIT 2 PPT.pdfUNIT 2 PPT.pdf
UNIT 2 PPT.pdf
 
Chapter 9 - Loops in C++
Chapter 9 - Loops in C++Chapter 9 - Loops in C++
Chapter 9 - Loops in C++
 
Loops Basics
Loops BasicsLoops Basics
Loops Basics
 
Loops in C.net.pptx
Loops in C.net.pptxLoops in C.net.pptx
Loops in C.net.pptx
 
175035-cse LAB-04
175035-cse LAB-04 175035-cse LAB-04
175035-cse LAB-04
 

More from yndaravind

Introduction to UML
Introduction to UMLIntroduction to UML
Introduction to UMLyndaravind
 
Classes and Objects
Classes and Objects  Classes and Objects
Classes and Objects yndaravind
 
The Object Model
The Object Model  The Object Model
The Object Model yndaravind
 
Selection & Making Decisions in c
Selection & Making Decisions in cSelection & Making Decisions in c
Selection & Making Decisions in cyndaravind
 
Bitwise Operators in C
Bitwise Operators in CBitwise Operators in C
Bitwise Operators in Cyndaravind
 
Structure In C
Structure In CStructure In C
Structure In Cyndaravind
 
Fnctions part2
Fnctions part2Fnctions part2
Fnctions part2yndaravind
 
Functions part1
Functions part1Functions part1
Functions part1yndaravind
 

More from yndaravind (14)

Introduction to UML
Introduction to UMLIntroduction to UML
Introduction to UML
 
Classes and Objects
Classes and Objects  Classes and Objects
Classes and Objects
 
The Object Model
The Object Model  The Object Model
The Object Model
 
OOAD
OOADOOAD
OOAD
 
OOAD
OOADOOAD
OOAD
 
FILES IN C
FILES IN CFILES IN C
FILES IN C
 
Selection & Making Decisions in c
Selection & Making Decisions in cSelection & Making Decisions in c
Selection & Making Decisions in c
 
Bitwise Operators in C
Bitwise Operators in CBitwise Operators in C
Bitwise Operators in C
 
Structure In C
Structure In CStructure In C
Structure In C
 
Strings part2
Strings part2Strings part2
Strings part2
 
Arrays In C
Arrays In CArrays In C
Arrays In C
 
Strings IN C
Strings IN CStrings IN C
Strings IN C
 
Fnctions part2
Fnctions part2Fnctions part2
Fnctions part2
 
Functions part1
Functions part1Functions part1
Functions part1
 

Recently uploaded

UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfNirmal Dwivedi
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structuredhanjurrannsibayan2
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Pooja Bhuva
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - Englishneillewis46
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxJisc
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Association for Project Management
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfDr Vijay Vishwakarma
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxJisc
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the ClassroomPooky Knightsmith
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsKarakKing
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024Elizabeth Walsh
 

Recently uploaded (20)

UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 

Repetations in C

  • 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