![Page 1: Repetition Algorithms · Repetition •Allows a program to execute a set of instructions over and over. •The term loop is a synonym for a repetition statement](https://reader034.vdocuments.mx/reader034/viewer/2022050510/5f9b4076bf24254e4807c541/html5/thumbnails/1.jpg)
Repetition Algorithms
![Page 2: Repetition Algorithms · Repetition •Allows a program to execute a set of instructions over and over. •The term loop is a synonym for a repetition statement](https://reader034.vdocuments.mx/reader034/viewer/2022050510/5f9b4076bf24254e4807c541/html5/thumbnails/2.jpg)
Repetition
• Allows a program to execute a set of instructions over and over.
• The term loop is a synonym for a repetition statement.
![Page 3: Repetition Algorithms · Repetition •Allows a program to execute a set of instructions over and over. •The term loop is a synonym for a repetition statement](https://reader034.vdocuments.mx/reader034/viewer/2022050510/5f9b4076bf24254e4807c541/html5/thumbnails/3.jpg)
A Repetition Example
Suppose that you have been asked to write a program that allows the user to enter 5 integers and displays the sum of the integers on the screen.
![Page 4: Repetition Algorithms · Repetition •Allows a program to execute a set of instructions over and over. •The term loop is a synonym for a repetition statement](https://reader034.vdocuments.mx/reader034/viewer/2022050510/5f9b4076bf24254e4807c541/html5/thumbnails/4.jpg)
A Repetition Example
Input Processing Output
5 integers
Num1
Num2
Num3
Num4
Num5
Display instructions
Get Num1, Num2, Num3, Num4 and
Num5
Calculate the Total
Display label and Total
Total
![Page 5: Repetition Algorithms · Repetition •Allows a program to execute a set of instructions over and over. •The term loop is a synonym for a repetition statement](https://reader034.vdocuments.mx/reader034/viewer/2022050510/5f9b4076bf24254e4807c541/html5/thumbnails/5.jpg)
A Repetition Example
AddFive
Display instructions
Get Num1, Num2, Num3, Num4, and Num5
Total = Num1 + Num2 + Num3 + Num4 + Num5
Display label and Total
End
![Page 6: Repetition Algorithms · Repetition •Allows a program to execute a set of instructions over and over. •The term loop is a synonym for a repetition statement](https://reader034.vdocuments.mx/reader034/viewer/2022050510/5f9b4076bf24254e4807c541/html5/thumbnails/6.jpg)
A Repetition Example - While
AddFive(While Version)
Display instructions
Total = 0
Counter = 1
While Counter <= 5
Get Num
Total = Total + Num
Counter = Counter + 1
End While
Display Total
End
![Page 7: Repetition Algorithms · Repetition •Allows a program to execute a set of instructions over and over. •The term loop is a synonym for a repetition statement](https://reader034.vdocuments.mx/reader034/viewer/2022050510/5f9b4076bf24254e4807c541/html5/thumbnails/7.jpg)
A Repetition Example
Total Counter Num
AddFive(While Version)
Display instructions
Total = 0
Counter = 1
While Counter <= 5
Get Num
Total = Total + Num
Counter = Counter + 1
End While
Display Total
End
![Page 8: Repetition Algorithms · Repetition •Allows a program to execute a set of instructions over and over. •The term loop is a synonym for a repetition statement](https://reader034.vdocuments.mx/reader034/viewer/2022050510/5f9b4076bf24254e4807c541/html5/thumbnails/8.jpg)
A Repetition Example - Until
AddFive(Until Version)
Display instructions
Total = 0
Counter = 1
Do until Counter = 6
Get Num
Total = Total + Num
Counter = Counter + 1
End do
Display Total
End
![Page 9: Repetition Algorithms · Repetition •Allows a program to execute a set of instructions over and over. •The term loop is a synonym for a repetition statement](https://reader034.vdocuments.mx/reader034/viewer/2022050510/5f9b4076bf24254e4807c541/html5/thumbnails/9.jpg)
A Repetition Example
Total Counter Num
AddFive(Until Version)
Display instructions
Total = 0
Counter = 1
Do until Counter = 6
Get Num
Total = Total + Num
Counter = Counter + 1
End do
Display Total
End
![Page 10: Repetition Algorithms · Repetition •Allows a program to execute a set of instructions over and over. •The term loop is a synonym for a repetition statement](https://reader034.vdocuments.mx/reader034/viewer/2022050510/5f9b4076bf24254e4807c541/html5/thumbnails/10.jpg)
A Repetition Example - For
AddFive(For Version)
Display instructions
Total = 0
For Counter = 1 to 5
Get Num
Total = Total + Num
End for
Display Total
End
![Page 11: Repetition Algorithms · Repetition •Allows a program to execute a set of instructions over and over. •The term loop is a synonym for a repetition statement](https://reader034.vdocuments.mx/reader034/viewer/2022050510/5f9b4076bf24254e4807c541/html5/thumbnails/11.jpg)
A Repetition Example
Total Counter Num
AddFive(For Version)
Display instructions
Total = 0
For Counter = 1 to 5
Get Num
Total = Total + Num
End for
Display Total
End
![Page 12: Repetition Algorithms · Repetition •Allows a program to execute a set of instructions over and over. •The term loop is a synonym for a repetition statement](https://reader034.vdocuments.mx/reader034/viewer/2022050510/5f9b4076bf24254e4807c541/html5/thumbnails/12.jpg)
A Repetition Example – While Exit
AddFive (While Exit Version)
Display instructions
Total = 0
Counter = 1
Do
Get Num
Total = Total + Num
Counter = Counter + 1
While Counter <= 5
Display Total
End
![Page 13: Repetition Algorithms · Repetition •Allows a program to execute a set of instructions over and over. •The term loop is a synonym for a repetition statement](https://reader034.vdocuments.mx/reader034/viewer/2022050510/5f9b4076bf24254e4807c541/html5/thumbnails/13.jpg)
A Repetition Example
Total Counter Num
AddFive (While Exit Version)
Display instructions
Total = 0
Counter = 1
Do
Get Num
Total = Total + Num
Counter = Counter + 1
While Counter <= 5
Display Total
End
![Page 14: Repetition Algorithms · Repetition •Allows a program to execute a set of instructions over and over. •The term loop is a synonym for a repetition statement](https://reader034.vdocuments.mx/reader034/viewer/2022050510/5f9b4076bf24254e4807c541/html5/thumbnails/14.jpg)
A Repetition Example – Until Exit
AddFive (Until Exit Version)
Display instructions
Total = 0
Counter = 1
Do
Get Num
Total = Total + Num
Counter = Counter + 1
Until Counter = 6
Display Total
End
![Page 15: Repetition Algorithms · Repetition •Allows a program to execute a set of instructions over and over. •The term loop is a synonym for a repetition statement](https://reader034.vdocuments.mx/reader034/viewer/2022050510/5f9b4076bf24254e4807c541/html5/thumbnails/15.jpg)
A Slightly Different Example
Assume that you have been asked to write a program that allows the user to enter and add positive integers. The user will enter any negative number when he/she is finished entering numbers and would like to see the result. The “dummy” number used to stop processing should not be added to the total.
![Page 16: Repetition Algorithms · Repetition •Allows a program to execute a set of instructions over and over. •The term loop is a synonym for a repetition statement](https://reader034.vdocuments.mx/reader034/viewer/2022050510/5f9b4076bf24254e4807c541/html5/thumbnails/16.jpg)
A Slightly Different Example
Input Processing Output
A set of positive
integers. Any negative
number can be used to
stop processing.
Display instructions
Repeat for each number
Get the number
Add number to total
Display label and Total
Total
![Page 17: Repetition Algorithms · Repetition •Allows a program to execute a set of instructions over and over. •The term loop is a synonym for a repetition statement](https://reader034.vdocuments.mx/reader034/viewer/2022050510/5f9b4076bf24254e4807c541/html5/thumbnails/17.jpg)
A Slightly Different Example
AddPositiveNumbers (While Version)
Display instructions
Total = 0
Get Num
Do while Num >= 0
Total = Total + Num
Get Num
End while
Display Total
End
![Page 18: Repetition Algorithms · Repetition •Allows a program to execute a set of instructions over and over. •The term loop is a synonym for a repetition statement](https://reader034.vdocuments.mx/reader034/viewer/2022050510/5f9b4076bf24254e4807c541/html5/thumbnails/18.jpg)
A Slightly Different Example
Total Num
AddPositiveNumbers (While Version)
Display instructions
Total = 0
Get Num
Do while Num >= 0
Total = Total + Num
Get Num
End while
Display Total
End
![Page 19: Repetition Algorithms · Repetition •Allows a program to execute a set of instructions over and over. •The term loop is a synonym for a repetition statement](https://reader034.vdocuments.mx/reader034/viewer/2022050510/5f9b4076bf24254e4807c541/html5/thumbnails/19.jpg)
A Slightly Different Example
AddPositiveNumbers (While Exit Version 1)
Display instructions
Total = 0
Do
Get Num
Total = Total + Num
While Num >= 0
Display Total
End
![Page 20: Repetition Algorithms · Repetition •Allows a program to execute a set of instructions over and over. •The term loop is a synonym for a repetition statement](https://reader034.vdocuments.mx/reader034/viewer/2022050510/5f9b4076bf24254e4807c541/html5/thumbnails/20.jpg)
A Slightly Different Example
Total Num
AddPositiveNumbers (While Exit Version)
Display instructions
Total = 0
Get Num
Do
Get Num
Total = Total + Num
While Num >= 0
Display Total
End
![Page 21: Repetition Algorithms · Repetition •Allows a program to execute a set of instructions over and over. •The term loop is a synonym for a repetition statement](https://reader034.vdocuments.mx/reader034/viewer/2022050510/5f9b4076bf24254e4807c541/html5/thumbnails/21.jpg)
A Slightly Different Example
AddPositiveNumbers (While Exit Version 2)
Display instructions
Total = 0
Do
Get Num
If Num >= 0 Then
Total = Total + Num
End if
While Num >= 0
Display Total
End
![Page 22: Repetition Algorithms · Repetition •Allows a program to execute a set of instructions over and over. •The term loop is a synonym for a repetition statement](https://reader034.vdocuments.mx/reader034/viewer/2022050510/5f9b4076bf24254e4807c541/html5/thumbnails/22.jpg)
A Slightly Different Example
Total Num
AddPositiveNumbers (While Exit Version 2)
Display instructions
Total = 0
Do
Get Num
If Num >= 0 Then
Total = Total + Num
End if
While Num >= 0
Display Total
End
![Page 23: Repetition Algorithms · Repetition •Allows a program to execute a set of instructions over and over. •The term loop is a synonym for a repetition statement](https://reader034.vdocuments.mx/reader034/viewer/2022050510/5f9b4076bf24254e4807c541/html5/thumbnails/23.jpg)
With Any Problem
• Follow the same process Can you ask clarifying questions?
Can you create an IPO chart?
Can you write an algorithm?• Can you do an example or describe the process
in English?
• Can you generalize that?
• Does any of the processing involve selection?
• Does any of the processing involve repetition?
![Page 24: Repetition Algorithms · Repetition •Allows a program to execute a set of instructions over and over. •The term loop is a synonym for a repetition statement](https://reader034.vdocuments.mx/reader034/viewer/2022050510/5f9b4076bf24254e4807c541/html5/thumbnails/24.jpg)
Practice Problem
Assume that you are creating a program that will count the number of students in a class who are getting an A. The user will enter the letter grade for each student in the class, one grade at a time and will enter an S when all grades have been entered. The program will display the number of A grades to the screen.
![Page 25: Repetition Algorithms · Repetition •Allows a program to execute a set of instructions over and over. •The term loop is a synonym for a repetition statement](https://reader034.vdocuments.mx/reader034/viewer/2022050510/5f9b4076bf24254e4807c541/html5/thumbnails/25.jpg)
Practice Problem
Assume that you are creating a program that will count the number of students in a class who are passing a course. The user will enter an integer value between 1 and 100 for each student in the class, one grade at a time and will enter a 0 when all grades have been entered. Students who score below 70 do not pass the course. The program will display the number of passing scores to the screen.
![Page 26: Repetition Algorithms · Repetition •Allows a program to execute a set of instructions over and over. •The term loop is a synonym for a repetition statement](https://reader034.vdocuments.mx/reader034/viewer/2022050510/5f9b4076bf24254e4807c541/html5/thumbnails/26.jpg)
A More Complex Repetition Problem
Assume that you are creating a program that will be used by customers to locate a specific movie in a video store. The customer should be allowed to enter the name of the movie and the program will display the location of the movie on the screen.
![Page 27: Repetition Algorithms · Repetition •Allows a program to execute a set of instructions over and over. •The term loop is a synonym for a repetition statement](https://reader034.vdocuments.mx/reader034/viewer/2022050510/5f9b4076bf24254e4807c541/html5/thumbnails/27.jpg)
A More Complex Repetition ProblemLookupMovie
Display instructions
Found = False
Get MovieTitle
Get first MovieRecord
Do Until Found or End Of File
If MovieTitle = MovieRecord.Title Then
Found = True
Else
Get nextMovieRecord
End if
End Do
If Found Then
Display label and MovieRecord.Location
Else
Display “Not Available” message
End If
End
![Page 28: Repetition Algorithms · Repetition •Allows a program to execute a set of instructions over and over. •The term loop is a synonym for a repetition statement](https://reader034.vdocuments.mx/reader034/viewer/2022050510/5f9b4076bf24254e4807c541/html5/thumbnails/28.jpg)
Practice Problem
Assume that you are creating a program that will determine if a number is a prime number. The user enters an integer between 4 and 100. The program prints either “Prime” or “Composite”.
![Page 29: Repetition Algorithms · Repetition •Allows a program to execute a set of instructions over and over. •The term loop is a synonym for a repetition statement](https://reader034.vdocuments.mx/reader034/viewer/2022050510/5f9b4076bf24254e4807c541/html5/thumbnails/29.jpg)
Practice Problem
Assume that you’ve been asked to write a program that displays a multiplication chart like the one given below. The user enters an integer that represents the “dimension” of the chart.
1 2 3 4
2 4 6 8
3 6 9 12
4 8 12 16