Introduction to Loops Iteration Repetition Counting Loops Also known as.
Week 5 - Friday. What did we talk about last time? Repetition while loops.
-
Upload
virginia-banks -
Category
Documents
-
view
212 -
download
0
Transcript of Week 5 - Friday. What did we talk about last time? Repetition while loops.
![Page 1: Week 5 - Friday. What did we talk about last time? Repetition while loops.](https://reader035.fdocuments.net/reader035/viewer/2022072006/56649d015503460f949d3efd/html5/thumbnails/1.jpg)
CS 121Week 5 - Friday
![Page 2: Week 5 - Friday. What did we talk about last time? Repetition while loops.](https://reader035.fdocuments.net/reader035/viewer/2022072006/56649d015503460f949d3efd/html5/thumbnails/2.jpg)
Last time
What did we talk about last time? Repetitionwhile loops
![Page 3: Week 5 - Friday. What did we talk about last time? Repetition while loops.](https://reader035.fdocuments.net/reader035/viewer/2022072006/56649d015503460f949d3efd/html5/thumbnails/3.jpg)
Questions?
![Page 4: Week 5 - Friday. What did we talk about last time? Repetition while loops.](https://reader035.fdocuments.net/reader035/viewer/2022072006/56649d015503460f949d3efd/html5/thumbnails/4.jpg)
Project 2
![Page 5: Week 5 - Friday. What did we talk about last time? Repetition while loops.](https://reader035.fdocuments.net/reader035/viewer/2022072006/56649d015503460f949d3efd/html5/thumbnails/5.jpg)
while Loop Examples
![Page 6: Week 5 - Friday. What did we talk about last time? Repetition while loops.](https://reader035.fdocuments.net/reader035/viewer/2022072006/56649d015503460f949d3efd/html5/thumbnails/6.jpg)
Anatomy of a while loop
while( condition ){
statement1;statement2;…statementn;
}
A whole bunch of
statements
![Page 7: Week 5 - Friday. What did we talk about last time? Repetition while loops.](https://reader035.fdocuments.net/reader035/viewer/2022072006/56649d015503460f949d3efd/html5/thumbnails/7.jpg)
Guessing game
Let’s say that you wanted to write a program to guess a number that a person had come up with
The number is between 1 and 100 Every time the computer guesses a
number, the person enters: H if the number is too high L if the number is too low F if the number was found
![Page 8: Week 5 - Friday. What did we talk about last time? Repetition while loops.](https://reader035.fdocuments.net/reader035/viewer/2022072006/56649d015503460f949d3efd/html5/thumbnails/8.jpg)
Guessing game algorithm
1. Start with the minimum and maximum of the range
2. Find the midpoint3. Ask the user if the midpoint is correct4. If the answer is too high, go to Step 1
using the minimum and the midpoint - 1 as the new range
5. If the answer is too low, go to Step 1 using the midpoint + 1 and the maximum as the new range
6. If the midpoint is correct, you’re done!
![Page 9: Week 5 - Friday. What did we talk about last time? Repetition while loops.](https://reader035.fdocuments.net/reader035/viewer/2022072006/56649d015503460f949d3efd/html5/thumbnails/9.jpg)
Nested loops
Just as with if-statements, it’s possible to nest loops
A repetitive task can be done inside of another repetitive task
Be careful! You can make the computer do a lot of work
![Page 10: Week 5 - Friday. What did we talk about last time? Repetition while loops.](https://reader035.fdocuments.net/reader035/viewer/2022072006/56649d015503460f949d3efd/html5/thumbnails/10.jpg)
Triangular numbers
Triangular numbers are 1, 3, 6, 10, … 1 = 1 3 = 1 + 2 6 = 1 + 2 + 3 10 + 1 + 2 + 3 + 4
Let’s write a program that expresses the nth triangular number by printing 1 on the first line, 1 and 2 on the second line, 1, 2, and 3 on the third line, and so on
![Page 11: Week 5 - Friday. What did we talk about last time? Repetition while loops.](https://reader035.fdocuments.net/reader035/viewer/2022072006/56649d015503460f949d3efd/html5/thumbnails/11.jpg)
Lab 5
![Page 12: Week 5 - Friday. What did we talk about last time? Repetition while loops.](https://reader035.fdocuments.net/reader035/viewer/2022072006/56649d015503460f949d3efd/html5/thumbnails/12.jpg)
Upcoming
![Page 13: Week 5 - Friday. What did we talk about last time? Repetition while loops.](https://reader035.fdocuments.net/reader035/viewer/2022072006/56649d015503460f949d3efd/html5/thumbnails/13.jpg)
Next time…
for loops More loop examples
![Page 14: Week 5 - Friday. What did we talk about last time? Repetition while loops.](https://reader035.fdocuments.net/reader035/viewer/2022072006/56649d015503460f949d3efd/html5/thumbnails/14.jpg)
Reminders
Keep reading Chapter 5 Keep working on Project 2