SlideShare a Scribd company logo
Repetition (1)
C Programming
AAST
CCIT
 What is a Loop?
 Count-Controlled Loops
 Event-Controlled Loops
 While Statement Syntax
 Using a While Statement for Summing
and Counting
 Nested While Loops
 Loop Testing and Debugging
 A loop is a repetition control structure.
 That is, you can execute particular
statements more than once in a
controlled fashion
 Statements are executed as long as
some condition remains true
count controlled loops
repeat a specified number of times
event-controlled loops
some condition within the loop body
changes and this causes the repeating to
stop
SYNTAX
while ( Expression )
{ .
. /*loop body */
.
}
NOTE: Loop body can be a single
statement, a null statement, or a block.
When the expression is tested and found to be
false, the loop is exited and control passes to
the statement which follows the loop body.
WHILE LOOP
FALSE
TRUE
body
statement
Expression
 Every while loop will always contain three main
elements:
 Priming: initialize your variables.
 Testing: test against some known condition.
 Updating: update the variable that is tested.
Parts of a While Loop
int count ;
count = 4; /* initialize loop variable */
while (count > 0) /* test expression */
{
printf(“ %d n ”,count ) ; /* repeated action */
count -- ; /*update loop variable */
}
printf( “Done” ) ;
1. Priming
1. Priming
2. Test Condition
2. Test Condition
3. Update
3. Update
int count ;
count = 4;
while (count > 0)
{
printf( " %d n " , count ) ;
count -- ;
}
printf( " Done " ) ;
OUTPUT
count
int count ;
count = 4;
while (count > 0)
{
printf( " %d n " , count ) ;
count -- ;
}
printf( " Done " ) ;
OUTPUT
count
4
int count ;
count = 4;
while (count > 0) TRUE
{
printf( " %d n " , count ) ;
count -- ;
}
printf( " Done " ) ;
OUTPUT
count
4
int count ;
count = 4;
while (count > 0)
{
printf( " %d n " , count ) ;
count -- ;
}
printf( " Done " ) ;
OUTPUT
4
count
4
int count ;
count = 4;
while (count > 0)
{
printf( " %d n " , count ) ;
count -- ;
}
printf( " Done " ) ;
OUTPUT
4
count
3
int count ;
count = 4;
while (count > 0) TRUE
{
printf( " %d n " , count ) ;
count -- ;
}
printf( " Done " ) ;
OUTPUT
4
count
3
int count ;
count = 4;
while (count > 0)
{
printf( " %d n " , count ) ;
count -- ;
}
printf( " Done " ) ;
OUTPUT
4
3
count
3
int count ;
count = 4;
while (count > 0)
{
printf( " %d n " , count ) ;
count -- ;
}
printf( " Done " ) ;
OUTPUT
4
3
count
2
int count ;
count = 4;
while (count > 0) TRUE
{
printf( " %d n " , count ) ;
count -- ;
}
printf( " Done " ) ;
OUTPUT
4
3
count
2
int count ;
count = 4;
while (count > 0)
{
printf( " %d n " , count ) ;
count -- ;
}
printf( " Done " ) ;
OUTPUT
4
3
2
count
2
int count ;
count = 4;
while (count > 0)
{
printf( " %d n " , count ) ;
count -- ;
}
printf( " Done " ) ;
OUTPUT
4
3
2
count
1
int count ;
count = 4;
while (count > 0) TRUE
{
printf( " %d n " , count ) ;
count -- ;
}
printf( " Done " ) ;
OUTPUT
4
3
2
count
1
int count ;
count = 4;
while (count > 0)
{
printf( " %d n " , count ) ;
count -- ;
}
printf( " Done " ) ;
OUTPUT
4
3
2
1
count
1
int count ;
count = 4;
while (count > 0)
{
printf( " %d n " , count ) ;
count -- ;
}
printf( " Done " ) ;
OUTPUT
4
3
2
1
count
0
int count ;
count = 4;
while (count > 0) FALSE
{
printf( " %d n " , count ) ;
count -- ;
}
printf( " Done " ) ;
OUTPUT
4
3
2
1
count
0
int count ;
count = 4;
while (count > 0)
{
printf( " %d n " , count ) ;
count -- ;
}
printf( " Done " ) ;
OUTPUT
4
3
2
1
Done
count
0
#include <stdio.h>
#define MAX 10
main ()
{
int index =1;
while (index <= MAX)
{
printf ("Index: %dn", index);
index = index + 1;
}
}
Simple While Loop
1. Priming
1. Priming
2. Test Condition
2. Test Condition
3. Update
3. Update
OUTPUT:
Index: 1
Index: 2
Index: 3
Index: 4
Index: 5
Index: 6
Index: 7
Index: 8
Index: 9
Index: 10
•Infinite Loop: A loop that never ends.
–Generally, you want to avoid these!
–There are special cases, however, when
you do want to create infinite loops on
purpose.
•Common Exam Questions:
–Given a piece of code, identify the bug in
the code.
–You may need to identify infinite loops.
Infinite loop
#include <stdio.h>
#define MAX 10
main ()
{
int index =1;
while (index <= MAX)
{
printf ("Index: %dn", index);
}
}
Infinite While Loop
1. Priming
1. Priming
2. Test Condition
2. Test Condition
3. Where is the
3. Where is the
update part
update part
Index: 1
Index: 1
Index: 1
Index: 1
Index: 1
… [forever]
#include <stdio.h>
/* no MAX here */
main ()
{
int index =1;
while (index > 0)
{
printf ("Index: %dn", index);
index = index + 1;
}
}
Infinite While Loop
1. Priming
1. Priming
2. Test Condition
2. Test Condition
3. Update
3. Update
Index: 1
Index: 2
Index: 3
Index: 4
Index: 5
… [forever]
Use a while loop to read 100
grades in an exam and find their
total.
Count-Controlled Loop Example
int thisGrade ;
int total=0 ;
int count ;
count = 1 ; /* initialize */
while ( count < 101 ) /* test expression */
{
printf(“ Student %d Grade : “, count );
scanf(“ %d “ , &thisGrade ) ;
total = total + thisGrade ;
count++ ; /* update */
}
printf(“The total = %d n“ , total ) ;
 Signals the end of data entry
 Also known as signal value, dummy value,
or flag value
 Also known as indefinite repetition
because the number of repetitions the
code will perform is unknown before the
loop
 How are they used?
 Programmer picks a value that would never be
encountered for normal data
 User enters normal data and then when done,
enters the unusual value
 The loop would stop when seeing the unusual
value
total = 0;
printf(“ Grade ( -1 to stop): “ );
scanf(“ %d “ , &thisGrade ) ;
while (thisGrade != -1)
{
total = total + thisGrade;
printf(“ Grade ( -1 to stop): “ );
scanf(“ %d “ , &thisGrade ) ;
}
printf(“ The total is %d n” ,total );
count = 0;
total = 0;
GoOn = 1; /* initialize flag */
while ( GoOn == 1 )
{
printf(“ Enter Grade “);
scanf(“ %d “ , &thisGrade );
if ( thisGrade == -1 )
GoOn = 0 ; /* change flag value */
else {
count++;
total = total + thisGrade ;
}
}
printf( “ Total is %d n “, total ) ;
 write a program that reads in the
grades of 50 students in a course (out
of 100 points each ) and then count the
number of A students ( grade > 85 ) and
the number of B students (grade > 75 ).
 Write a program that does a survey on a certain
question. The question has 3 possible answers.
Run the survey on 20 people and then display
the number of people who chose each answer.
 Example:
What is your favorite subject?
A. Mathematics
B. Economics
C. Programming
Is a looping control structure in which the loop
condition is tested after each iteration of the
loop.
SYNTAX
do
{
Statements
} while ( Expression ) ;
Loop body statement can be a single statement or a block.
char more ;
int thisGrade , total;
total = 0 ;
do
{
printf(“ Grade : “ ) ;
scanf(“ %d “ , &thisGrade ) ;
total = total + thisGrade ;
printf(“ Any more students ? (Y/N) “ ) ;
scanf(“ %c “ , &more ) ;
} while ( more == ‘y’ || more == ‘Y’ ) ;
printf ( “ Total = %d “ , total );
Grades Example
 POST-TEST loop
(exit-condition)
 The looping condition
is tested after
executing the loop
body.
 Loop body is always
executed at least
once.
 PRE-TEST loop
(entry-condition)
 The looping condition
is tested before
executing the loop
body.
 Loop body may not
be executed at all.
When the expression is tested and found to be false,
the loop is exited and control passes to the
statement that follows the do-while statement.
Statement
Expression
DO
WHILE
FALSE
TRUE
 Assume you put 1000 pounds in a projects that
returns a profit of about 5% per year. How long
will it take for your money to double ?
 Assume you put 5000 pounds in a projects that
returns a profit of about 10% per year. How
much money will you have in 5 years ?
Ad

More Related Content

Similar to Lecture 13 Loops1 with C++ programming.PPT (20)

C Programming: Control Structure
C Programming: Control StructureC Programming: Control Structure
C Programming: Control Structure
Sokngim Sa
 
04-Looping( For , while and do while looping) .pdf
04-Looping( For , while and do while looping) .pdf04-Looping( For , while and do while looping) .pdf
04-Looping( For , while and do while looping) .pdf
nithishkumar2867
 
computer programming Control Statements.pptx
computer programming Control Statements.pptxcomputer programming Control Statements.pptx
computer programming Control Statements.pptx
eaglesniper008
 
INPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN C
INPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN CINPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN C
INPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN C
Dr. Chandrakant Divate
 
5 c control statements looping
5  c control statements looping5  c control statements looping
5 c control statements looping
MomenMostafa
 
Control Structures in C
Control Structures in CControl Structures in C
Control Structures in C
sana shaikh
 
C Programming Lab.pdf
C Programming Lab.pdfC Programming Lab.pdf
C Programming Lab.pdf
MOJO89
 
Bsit1
Bsit1Bsit1
Bsit1
jigeno
 
SPL 8 | Loop Statements in C
SPL 8 | Loop Statements in CSPL 8 | Loop Statements in C
SPL 8 | Loop Statements in C
Mohammad Imam Hossain
 
Introduction to c part -1
Introduction to c   part -1Introduction to c   part -1
Introduction to c part -1
baabtra.com - No. 1 supplier of quality freshers
 
Slide07 repetitions
Slide07 repetitionsSlide07 repetitions
Slide07 repetitions
altwirqi
 
4 operators, expressions &amp; statements
4  operators, expressions &amp; statements4  operators, expressions &amp; statements
4 operators, expressions &amp; statements
MomenMostafa
 
Cs291 assignment solution
Cs291 assignment solutionCs291 assignment solution
Cs291 assignment solution
Kuntal Bhowmick
 
COM1407: Program Control Structures – Repetition and Loops
COM1407: Program Control Structures – Repetition and Loops COM1407: Program Control Structures – Repetition and Loops
COM1407: Program Control Structures – Repetition and Loops
Hemantha Kulathilake
 
Loops in c
Loops in cLoops in c
Loops in c
shubhampandav3
 
Branching statements
Branching statementsBranching statements
Branching statements
ArunMK17
 
CONTROL FLOW in C.pptx
CONTROL FLOW in C.pptxCONTROL FLOW in C.pptx
CONTROL FLOW in C.pptx
SmitaAparadh
 
Fundamental of Information Technology - UNIT 8
Fundamental of Information Technology - UNIT 8Fundamental of Information Technology - UNIT 8
Fundamental of Information Technology - UNIT 8
Shipra Swati
 
C important questions
C important questionsC important questions
C important questions
JYOTI RANJAN PAL
 
Part 1 1)#include stdio.hint testWhileLoop() ; int testFo.pdf
Part 1 1)#include stdio.hint testWhileLoop() ; int testFo.pdfPart 1 1)#include stdio.hint testWhileLoop() ; int testFo.pdf
Part 1 1)#include stdio.hint testWhileLoop() ; int testFo.pdf
Lalkamal2
 
C Programming: Control Structure
C Programming: Control StructureC Programming: Control Structure
C Programming: Control Structure
Sokngim Sa
 
04-Looping( For , while and do while looping) .pdf
04-Looping( For , while and do while looping) .pdf04-Looping( For , while and do while looping) .pdf
04-Looping( For , while and do while looping) .pdf
nithishkumar2867
 
computer programming Control Statements.pptx
computer programming Control Statements.pptxcomputer programming Control Statements.pptx
computer programming Control Statements.pptx
eaglesniper008
 
INPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN C
INPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN CINPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN C
INPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN C
Dr. Chandrakant Divate
 
5 c control statements looping
5  c control statements looping5  c control statements looping
5 c control statements looping
MomenMostafa
 
Control Structures in C
Control Structures in CControl Structures in C
Control Structures in C
sana shaikh
 
C Programming Lab.pdf
C Programming Lab.pdfC Programming Lab.pdf
C Programming Lab.pdf
MOJO89
 
Slide07 repetitions
Slide07 repetitionsSlide07 repetitions
Slide07 repetitions
altwirqi
 
4 operators, expressions &amp; statements
4  operators, expressions &amp; statements4  operators, expressions &amp; statements
4 operators, expressions &amp; statements
MomenMostafa
 
Cs291 assignment solution
Cs291 assignment solutionCs291 assignment solution
Cs291 assignment solution
Kuntal Bhowmick
 
COM1407: Program Control Structures – Repetition and Loops
COM1407: Program Control Structures – Repetition and Loops COM1407: Program Control Structures – Repetition and Loops
COM1407: Program Control Structures – Repetition and Loops
Hemantha Kulathilake
 
Branching statements
Branching statementsBranching statements
Branching statements
ArunMK17
 
CONTROL FLOW in C.pptx
CONTROL FLOW in C.pptxCONTROL FLOW in C.pptx
CONTROL FLOW in C.pptx
SmitaAparadh
 
Fundamental of Information Technology - UNIT 8
Fundamental of Information Technology - UNIT 8Fundamental of Information Technology - UNIT 8
Fundamental of Information Technology - UNIT 8
Shipra Swati
 
Part 1 1)#include stdio.hint testWhileLoop() ; int testFo.pdf
Part 1 1)#include stdio.hint testWhileLoop() ; int testFo.pdfPart 1 1)#include stdio.hint testWhileLoop() ; int testFo.pdf
Part 1 1)#include stdio.hint testWhileLoop() ; int testFo.pdf
Lalkamal2
 

More from SamahAdel16 (8)

Discrete Structures_Logic and Proofs_Saleh.ppt
Discrete Structures_Logic and Proofs_Saleh.pptDiscrete Structures_Logic and Proofs_Saleh.ppt
Discrete Structures_Logic and Proofs_Saleh.ppt
SamahAdel16
 
Discrete Structures_Relations_Lec 1.pptx
Discrete Structures_Relations_Lec 1.pptxDiscrete Structures_Relations_Lec 1.pptx
Discrete Structures_Relations_Lec 1.pptx
SamahAdel16
 
Computer organization and Architecture Chapter 2-Lec2.pptx
Computer organization and Architecture  Chapter 2-Lec2.pptxComputer organization and Architecture  Chapter 2-Lec2.pptx
Computer organization and Architecture Chapter 2-Lec2.pptx
SamahAdel16
 
Computer organization and Architecture Chapter 1.pptx
Computer organization and Architecture  Chapter 1.pptxComputer organization and Architecture  Chapter 1.pptx
Computer organization and Architecture Chapter 1.pptx
SamahAdel16
 
Lecture 15 Arrays with C++ programming.ppt
Lecture 15  Arrays with C++ programming.pptLecture 15  Arrays with C++ programming.ppt
Lecture 15 Arrays with C++ programming.ppt
SamahAdel16
 
New 03A Short Version New Trends in Computer Science.pptx
New 03A Short Version New Trends in Computer Science.pptxNew 03A Short Version New Trends in Computer Science.pptx
New 03A Short Version New Trends in Computer Science.pptx
SamahAdel16
 
New 02-New Trends in Computer Science.pptx
New 02-New Trends in Computer Science.pptxNew 02-New Trends in Computer Science.pptx
New 02-New Trends in Computer Science.pptx
SamahAdel16
 
An Introduction to Computin course lecture 1.pptx
An Introduction to Computin course lecture 1.pptxAn Introduction to Computin course lecture 1.pptx
An Introduction to Computin course lecture 1.pptx
SamahAdel16
 
Discrete Structures_Logic and Proofs_Saleh.ppt
Discrete Structures_Logic and Proofs_Saleh.pptDiscrete Structures_Logic and Proofs_Saleh.ppt
Discrete Structures_Logic and Proofs_Saleh.ppt
SamahAdel16
 
Discrete Structures_Relations_Lec 1.pptx
Discrete Structures_Relations_Lec 1.pptxDiscrete Structures_Relations_Lec 1.pptx
Discrete Structures_Relations_Lec 1.pptx
SamahAdel16
 
Computer organization and Architecture Chapter 2-Lec2.pptx
Computer organization and Architecture  Chapter 2-Lec2.pptxComputer organization and Architecture  Chapter 2-Lec2.pptx
Computer organization and Architecture Chapter 2-Lec2.pptx
SamahAdel16
 
Computer organization and Architecture Chapter 1.pptx
Computer organization and Architecture  Chapter 1.pptxComputer organization and Architecture  Chapter 1.pptx
Computer organization and Architecture Chapter 1.pptx
SamahAdel16
 
Lecture 15 Arrays with C++ programming.ppt
Lecture 15  Arrays with C++ programming.pptLecture 15  Arrays with C++ programming.ppt
Lecture 15 Arrays with C++ programming.ppt
SamahAdel16
 
New 03A Short Version New Trends in Computer Science.pptx
New 03A Short Version New Trends in Computer Science.pptxNew 03A Short Version New Trends in Computer Science.pptx
New 03A Short Version New Trends in Computer Science.pptx
SamahAdel16
 
New 02-New Trends in Computer Science.pptx
New 02-New Trends in Computer Science.pptxNew 02-New Trends in Computer Science.pptx
New 02-New Trends in Computer Science.pptx
SamahAdel16
 
An Introduction to Computin course lecture 1.pptx
An Introduction to Computin course lecture 1.pptxAn Introduction to Computin course lecture 1.pptx
An Introduction to Computin course lecture 1.pptx
SamahAdel16
 
Ad

Recently uploaded (15)

美国文凭明尼苏达大学莫里斯分校毕业证范本UMM学位证书
美国文凭明尼苏达大学莫里斯分校毕业证范本UMM学位证书美国文凭明尼苏达大学莫里斯分校毕业证范本UMM学位证书
美国文凭明尼苏达大学莫里斯分校毕业证范本UMM学位证书
Taqyea
 
The Hidden Risks of Hiring Hackers to Change Grades: An Awareness Guide
The Hidden Risks of Hiring Hackers to Change Grades: An Awareness GuideThe Hidden Risks of Hiring Hackers to Change Grades: An Awareness Guide
The Hidden Risks of Hiring Hackers to Change Grades: An Awareness Guide
russellpeter1995
 
Breaking Down the Latest Spectrum Internet Plans.pdf
Breaking Down the Latest Spectrum Internet Plans.pdfBreaking Down the Latest Spectrum Internet Plans.pdf
Breaking Down the Latest Spectrum Internet Plans.pdf
Internet Bundle Now
 
CompTIA-Security-Study-Guide-with-over-500-Practice-Test-Questions-Exam-SY0-7...
CompTIA-Security-Study-Guide-with-over-500-Practice-Test-Questions-Exam-SY0-7...CompTIA-Security-Study-Guide-with-over-500-Practice-Test-Questions-Exam-SY0-7...
CompTIA-Security-Study-Guide-with-over-500-Practice-Test-Questions-Exam-SY0-7...
emestica1
 
学生卡英国RCA毕业证皇家艺术学院电子毕业证学历证书
学生卡英国RCA毕业证皇家艺术学院电子毕业证学历证书学生卡英国RCA毕业证皇家艺术学院电子毕业证学历证书
学生卡英国RCA毕业证皇家艺术学院电子毕业证学历证书
Taqyea
 
ProjectArtificial Intelligence Good or Evil.pptx
ProjectArtificial Intelligence Good or Evil.pptxProjectArtificial Intelligence Good or Evil.pptx
ProjectArtificial Intelligence Good or Evil.pptx
OlenaKotovska
 
Presentation Mehdi Monitorama 2022 Cancer and Monitoring
Presentation Mehdi Monitorama 2022 Cancer and MonitoringPresentation Mehdi Monitorama 2022 Cancer and Monitoring
Presentation Mehdi Monitorama 2022 Cancer and Monitoring
mdaoudi
 
DEF CON 25 - Whitney-Merrill-and-Terrell-McSweeny-Tick-Tick-Boom-Tech-and-the...
DEF CON 25 - Whitney-Merrill-and-Terrell-McSweeny-Tick-Tick-Boom-Tech-and-the...DEF CON 25 - Whitney-Merrill-and-Terrell-McSweeny-Tick-Tick-Boom-Tech-and-the...
DEF CON 25 - Whitney-Merrill-and-Terrell-McSweeny-Tick-Tick-Boom-Tech-and-the...
werhkr1
 
introduction to html and cssIntroHTML.ppt
introduction to html and cssIntroHTML.pptintroduction to html and cssIntroHTML.ppt
introduction to html and cssIntroHTML.ppt
SherifElGohary7
 
AG-FIRMA Ai Agent for Agriculture | RAG ..
AG-FIRMA Ai Agent for Agriculture  | RAG ..AG-FIRMA Ai Agent for Agriculture  | RAG ..
AG-FIRMA Ai Agent for Agriculture | RAG ..
Anass Nabil
 
GiacomoVacca - WebRTC - troubleshooting media negotiation.pdf
GiacomoVacca - WebRTC - troubleshooting media negotiation.pdfGiacomoVacca - WebRTC - troubleshooting media negotiation.pdf
GiacomoVacca - WebRTC - troubleshooting media negotiation.pdf
Giacomo Vacca
 
IoT PPT introduction to internet of things
IoT PPT introduction to internet of thingsIoT PPT introduction to internet of things
IoT PPT introduction to internet of things
VaishnaviPatil3995
 
Cloud-to-cloud Migration presentation.pptx
Cloud-to-cloud Migration presentation.pptxCloud-to-cloud Migration presentation.pptx
Cloud-to-cloud Migration presentation.pptx
marketing140789
 
Paper: World Game (s) Great Redesign.pdf
Paper: World Game (s) Great Redesign.pdfPaper: World Game (s) Great Redesign.pdf
Paper: World Game (s) Great Redesign.pdf
Steven McGee
 
How to Install & Activate ListGrabber - eGrabber
How to Install & Activate ListGrabber - eGrabberHow to Install & Activate ListGrabber - eGrabber
How to Install & Activate ListGrabber - eGrabber
eGrabber
 
美国文凭明尼苏达大学莫里斯分校毕业证范本UMM学位证书
美国文凭明尼苏达大学莫里斯分校毕业证范本UMM学位证书美国文凭明尼苏达大学莫里斯分校毕业证范本UMM学位证书
美国文凭明尼苏达大学莫里斯分校毕业证范本UMM学位证书
Taqyea
 
The Hidden Risks of Hiring Hackers to Change Grades: An Awareness Guide
The Hidden Risks of Hiring Hackers to Change Grades: An Awareness GuideThe Hidden Risks of Hiring Hackers to Change Grades: An Awareness Guide
The Hidden Risks of Hiring Hackers to Change Grades: An Awareness Guide
russellpeter1995
 
Breaking Down the Latest Spectrum Internet Plans.pdf
Breaking Down the Latest Spectrum Internet Plans.pdfBreaking Down the Latest Spectrum Internet Plans.pdf
Breaking Down the Latest Spectrum Internet Plans.pdf
Internet Bundle Now
 
CompTIA-Security-Study-Guide-with-over-500-Practice-Test-Questions-Exam-SY0-7...
CompTIA-Security-Study-Guide-with-over-500-Practice-Test-Questions-Exam-SY0-7...CompTIA-Security-Study-Guide-with-over-500-Practice-Test-Questions-Exam-SY0-7...
CompTIA-Security-Study-Guide-with-over-500-Practice-Test-Questions-Exam-SY0-7...
emestica1
 
学生卡英国RCA毕业证皇家艺术学院电子毕业证学历证书
学生卡英国RCA毕业证皇家艺术学院电子毕业证学历证书学生卡英国RCA毕业证皇家艺术学院电子毕业证学历证书
学生卡英国RCA毕业证皇家艺术学院电子毕业证学历证书
Taqyea
 
ProjectArtificial Intelligence Good or Evil.pptx
ProjectArtificial Intelligence Good or Evil.pptxProjectArtificial Intelligence Good or Evil.pptx
ProjectArtificial Intelligence Good or Evil.pptx
OlenaKotovska
 
Presentation Mehdi Monitorama 2022 Cancer and Monitoring
Presentation Mehdi Monitorama 2022 Cancer and MonitoringPresentation Mehdi Monitorama 2022 Cancer and Monitoring
Presentation Mehdi Monitorama 2022 Cancer and Monitoring
mdaoudi
 
DEF CON 25 - Whitney-Merrill-and-Terrell-McSweeny-Tick-Tick-Boom-Tech-and-the...
DEF CON 25 - Whitney-Merrill-and-Terrell-McSweeny-Tick-Tick-Boom-Tech-and-the...DEF CON 25 - Whitney-Merrill-and-Terrell-McSweeny-Tick-Tick-Boom-Tech-and-the...
DEF CON 25 - Whitney-Merrill-and-Terrell-McSweeny-Tick-Tick-Boom-Tech-and-the...
werhkr1
 
introduction to html and cssIntroHTML.ppt
introduction to html and cssIntroHTML.pptintroduction to html and cssIntroHTML.ppt
introduction to html and cssIntroHTML.ppt
SherifElGohary7
 
AG-FIRMA Ai Agent for Agriculture | RAG ..
AG-FIRMA Ai Agent for Agriculture  | RAG ..AG-FIRMA Ai Agent for Agriculture  | RAG ..
AG-FIRMA Ai Agent for Agriculture | RAG ..
Anass Nabil
 
GiacomoVacca - WebRTC - troubleshooting media negotiation.pdf
GiacomoVacca - WebRTC - troubleshooting media negotiation.pdfGiacomoVacca - WebRTC - troubleshooting media negotiation.pdf
GiacomoVacca - WebRTC - troubleshooting media negotiation.pdf
Giacomo Vacca
 
IoT PPT introduction to internet of things
IoT PPT introduction to internet of thingsIoT PPT introduction to internet of things
IoT PPT introduction to internet of things
VaishnaviPatil3995
 
Cloud-to-cloud Migration presentation.pptx
Cloud-to-cloud Migration presentation.pptxCloud-to-cloud Migration presentation.pptx
Cloud-to-cloud Migration presentation.pptx
marketing140789
 
Paper: World Game (s) Great Redesign.pdf
Paper: World Game (s) Great Redesign.pdfPaper: World Game (s) Great Redesign.pdf
Paper: World Game (s) Great Redesign.pdf
Steven McGee
 
How to Install & Activate ListGrabber - eGrabber
How to Install & Activate ListGrabber - eGrabberHow to Install & Activate ListGrabber - eGrabber
How to Install & Activate ListGrabber - eGrabber
eGrabber
 
Ad

Lecture 13 Loops1 with C++ programming.PPT

  • 2.  What is a Loop?  Count-Controlled Loops  Event-Controlled Loops  While Statement Syntax  Using a While Statement for Summing and Counting  Nested While Loops  Loop Testing and Debugging
  • 3.  A loop is a repetition control structure.  That is, you can execute particular statements more than once in a controlled fashion  Statements are executed as long as some condition remains true
  • 4. count controlled loops repeat a specified number of times event-controlled loops some condition within the loop body changes and this causes the repeating to stop
  • 5. SYNTAX while ( Expression ) { . . /*loop body */ . } NOTE: Loop body can be a single statement, a null statement, or a block.
  • 6. When the expression is tested and found to be false, the loop is exited and control passes to the statement which follows the loop body. WHILE LOOP FALSE TRUE body statement Expression
  • 7.  Every while loop will always contain three main elements:  Priming: initialize your variables.  Testing: test against some known condition.  Updating: update the variable that is tested. Parts of a While Loop
  • 8. int count ; count = 4; /* initialize loop variable */ while (count > 0) /* test expression */ { printf(“ %d n ”,count ) ; /* repeated action */ count -- ; /*update loop variable */ } printf( “Done” ) ; 1. Priming 1. Priming 2. Test Condition 2. Test Condition 3. Update 3. Update
  • 9. int count ; count = 4; while (count > 0) { printf( " %d n " , count ) ; count -- ; } printf( " Done " ) ; OUTPUT count
  • 10. int count ; count = 4; while (count > 0) { printf( " %d n " , count ) ; count -- ; } printf( " Done " ) ; OUTPUT count 4
  • 11. int count ; count = 4; while (count > 0) TRUE { printf( " %d n " , count ) ; count -- ; } printf( " Done " ) ; OUTPUT count 4
  • 12. int count ; count = 4; while (count > 0) { printf( " %d n " , count ) ; count -- ; } printf( " Done " ) ; OUTPUT 4 count 4
  • 13. int count ; count = 4; while (count > 0) { printf( " %d n " , count ) ; count -- ; } printf( " Done " ) ; OUTPUT 4 count 3
  • 14. int count ; count = 4; while (count > 0) TRUE { printf( " %d n " , count ) ; count -- ; } printf( " Done " ) ; OUTPUT 4 count 3
  • 15. int count ; count = 4; while (count > 0) { printf( " %d n " , count ) ; count -- ; } printf( " Done " ) ; OUTPUT 4 3 count 3
  • 16. int count ; count = 4; while (count > 0) { printf( " %d n " , count ) ; count -- ; } printf( " Done " ) ; OUTPUT 4 3 count 2
  • 17. int count ; count = 4; while (count > 0) TRUE { printf( " %d n " , count ) ; count -- ; } printf( " Done " ) ; OUTPUT 4 3 count 2
  • 18. int count ; count = 4; while (count > 0) { printf( " %d n " , count ) ; count -- ; } printf( " Done " ) ; OUTPUT 4 3 2 count 2
  • 19. int count ; count = 4; while (count > 0) { printf( " %d n " , count ) ; count -- ; } printf( " Done " ) ; OUTPUT 4 3 2 count 1
  • 20. int count ; count = 4; while (count > 0) TRUE { printf( " %d n " , count ) ; count -- ; } printf( " Done " ) ; OUTPUT 4 3 2 count 1
  • 21. int count ; count = 4; while (count > 0) { printf( " %d n " , count ) ; count -- ; } printf( " Done " ) ; OUTPUT 4 3 2 1 count 1
  • 22. int count ; count = 4; while (count > 0) { printf( " %d n " , count ) ; count -- ; } printf( " Done " ) ; OUTPUT 4 3 2 1 count 0
  • 23. int count ; count = 4; while (count > 0) FALSE { printf( " %d n " , count ) ; count -- ; } printf( " Done " ) ; OUTPUT 4 3 2 1 count 0
  • 24. int count ; count = 4; while (count > 0) { printf( " %d n " , count ) ; count -- ; } printf( " Done " ) ; OUTPUT 4 3 2 1 Done count 0
  • 25. #include <stdio.h> #define MAX 10 main () { int index =1; while (index <= MAX) { printf ("Index: %dn", index); index = index + 1; } } Simple While Loop 1. Priming 1. Priming 2. Test Condition 2. Test Condition 3. Update 3. Update OUTPUT: Index: 1 Index: 2 Index: 3 Index: 4 Index: 5 Index: 6 Index: 7 Index: 8 Index: 9 Index: 10
  • 26. •Infinite Loop: A loop that never ends. –Generally, you want to avoid these! –There are special cases, however, when you do want to create infinite loops on purpose. •Common Exam Questions: –Given a piece of code, identify the bug in the code. –You may need to identify infinite loops. Infinite loop
  • 27. #include <stdio.h> #define MAX 10 main () { int index =1; while (index <= MAX) { printf ("Index: %dn", index); } } Infinite While Loop 1. Priming 1. Priming 2. Test Condition 2. Test Condition 3. Where is the 3. Where is the update part update part Index: 1 Index: 1 Index: 1 Index: 1 Index: 1 … [forever]
  • 28. #include <stdio.h> /* no MAX here */ main () { int index =1; while (index > 0) { printf ("Index: %dn", index); index = index + 1; } } Infinite While Loop 1. Priming 1. Priming 2. Test Condition 2. Test Condition 3. Update 3. Update Index: 1 Index: 2 Index: 3 Index: 4 Index: 5 … [forever]
  • 29. Use a while loop to read 100 grades in an exam and find their total. Count-Controlled Loop Example
  • 30. int thisGrade ; int total=0 ; int count ; count = 1 ; /* initialize */ while ( count < 101 ) /* test expression */ { printf(“ Student %d Grade : “, count ); scanf(“ %d “ , &thisGrade ) ; total = total + thisGrade ; count++ ; /* update */ } printf(“The total = %d n“ , total ) ;
  • 31.  Signals the end of data entry  Also known as signal value, dummy value, or flag value  Also known as indefinite repetition because the number of repetitions the code will perform is unknown before the loop
  • 32.  How are they used?  Programmer picks a value that would never be encountered for normal data  User enters normal data and then when done, enters the unusual value  The loop would stop when seeing the unusual value
  • 33. total = 0; printf(“ Grade ( -1 to stop): “ ); scanf(“ %d “ , &thisGrade ) ; while (thisGrade != -1) { total = total + thisGrade; printf(“ Grade ( -1 to stop): “ ); scanf(“ %d “ , &thisGrade ) ; } printf(“ The total is %d n” ,total );
  • 34. count = 0; total = 0; GoOn = 1; /* initialize flag */ while ( GoOn == 1 ) { printf(“ Enter Grade “); scanf(“ %d “ , &thisGrade ); if ( thisGrade == -1 ) GoOn = 0 ; /* change flag value */ else { count++; total = total + thisGrade ; } } printf( “ Total is %d n “, total ) ;
  • 35.  write a program that reads in the grades of 50 students in a course (out of 100 points each ) and then count the number of A students ( grade > 85 ) and the number of B students (grade > 75 ).
  • 36.  Write a program that does a survey on a certain question. The question has 3 possible answers. Run the survey on 20 people and then display the number of people who chose each answer.  Example: What is your favorite subject? A. Mathematics B. Economics C. Programming
  • 37. Is a looping control structure in which the loop condition is tested after each iteration of the loop. SYNTAX do { Statements } while ( Expression ) ; Loop body statement can be a single statement or a block.
  • 38. char more ; int thisGrade , total; total = 0 ; do { printf(“ Grade : “ ) ; scanf(“ %d “ , &thisGrade ) ; total = total + thisGrade ; printf(“ Any more students ? (Y/N) “ ) ; scanf(“ %c “ , &more ) ; } while ( more == ‘y’ || more == ‘Y’ ) ; printf ( “ Total = %d “ , total ); Grades Example
  • 39.  POST-TEST loop (exit-condition)  The looping condition is tested after executing the loop body.  Loop body is always executed at least once.  PRE-TEST loop (entry-condition)  The looping condition is tested before executing the loop body.  Loop body may not be executed at all.
  • 40. When the expression is tested and found to be false, the loop is exited and control passes to the statement that follows the do-while statement. Statement Expression DO WHILE FALSE TRUE
  • 41.  Assume you put 1000 pounds in a projects that returns a profit of about 5% per year. How long will it take for your money to double ?  Assume you put 5000 pounds in a projects that returns a profit of about 10% per year. How much money will you have in 5 years ?
  翻译: