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

BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room servicediscovermytutordmt
 
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...PsychoTech Services
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfAyushMahapatra5
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024Janet Corral
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...fonyou31
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingTeacherCyreneCayanan
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 

Recently uploaded (20)

BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 

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