loops robin burke it 130. outline announcement: homework #6 conditionals (review) iteration while...
TRANSCRIPT
![Page 1: Loops Robin Burke IT 130. Outline Announcement: Homework #6 Conditionals (review) Iteration while loop while with counter for loops](https://reader037.vdocuments.net/reader037/viewer/2022103101/5697bfc61a28abf838ca7166/html5/thumbnails/1.jpg)
Loops
Robin Burke
IT 130
![Page 2: Loops Robin Burke IT 130. Outline Announcement: Homework #6 Conditionals (review) Iteration while loop while with counter for loops](https://reader037.vdocuments.net/reader037/viewer/2022103101/5697bfc61a28abf838ca7166/html5/thumbnails/2.jpg)
Outline
Announcement: Homework #6 Conditionals (review) Iteration
while loop while with counter for loops
![Page 3: Loops Robin Burke IT 130. Outline Announcement: Homework #6 Conditionals (review) Iteration while loop while with counter for loops](https://reader037.vdocuments.net/reader037/viewer/2022103101/5697bfc61a28abf838ca7166/html5/thumbnails/3.jpg)
Homework #6
Report will be part of portfolio should be submitted by both partners put an electronic version on the web server
But revision only of the report don't re-design pages don't re-do experiments
You will get a detailed description of the portfolio assignment as it gets closer
![Page 4: Loops Robin Burke IT 130. Outline Announcement: Homework #6 Conditionals (review) Iteration while loop while with counter for loops](https://reader037.vdocuments.net/reader037/viewer/2022103101/5697bfc61a28abf838ca7166/html5/thumbnails/4.jpg)
If statements
Control of sequence of execution boolean expression value of expression controls what subsequent
code is executed Two forms
if (condition) { then part } if (condition) { then part } else { else part }
![Page 5: Loops Robin Burke IT 130. Outline Announcement: Homework #6 Conditionals (review) Iteration while loop while with counter for loops](https://reader037.vdocuments.net/reader037/viewer/2022103101/5697bfc61a28abf838ca7166/html5/thumbnails/5.jpg)
Iteration
Instructions on a shampoo bottle put on hair lather rinse repeat
We call this "iteration" executing some action repeatedly usually not forever, but according to some
algorithm
![Page 6: Loops Robin Burke IT 130. Outline Announcement: Homework #6 Conditionals (review) Iteration while loop while with counter for loops](https://reader037.vdocuments.net/reader037/viewer/2022103101/5697bfc61a28abf838ca7166/html5/thumbnails/6.jpg)
Examples
Roll the dice until you make your point or get a 7
Calculate a grade for each student in the class
Examine each word in a document, looking for one that is misspelled
![Page 7: Loops Robin Burke IT 130. Outline Announcement: Homework #6 Conditionals (review) Iteration while loop while with counter for loops](https://reader037.vdocuments.net/reader037/viewer/2022103101/5697bfc61a28abf838ca7166/html5/thumbnails/7.jpg)
JavaScript constructs
while loop for loop
![Page 8: Loops Robin Burke IT 130. Outline Announcement: Homework #6 Conditionals (review) Iteration while loop while with counter for loops](https://reader037.vdocuments.net/reader037/viewer/2022103101/5697bfc61a28abf838ca7166/html5/thumbnails/8.jpg)
while
Syntaxwhile (condition)
{
... body ...
}
Meaning if the condition is true, execute the body if the condition is still true, do it again etc. if the condition ever becomes false, stop
![Page 9: Loops Robin Burke IT 130. Outline Announcement: Homework #6 Conditionals (review) Iteration while loop while with counter for loops](https://reader037.vdocuments.net/reader037/viewer/2022103101/5697bfc61a28abf838ca7166/html5/thumbnails/9.jpg)
Comparison of if and while Appearance is similar
if (condition){
... body ...}while (condition){
... body ...}
Meaning is similar true condition means body is executed
Difference is in repetition body in if statement is executed at most once body in while loop is repeatedly executed until condition is false
![Page 10: Loops Robin Burke IT 130. Outline Announcement: Homework #6 Conditionals (review) Iteration while loop while with counter for loops](https://reader037.vdocuments.net/reader037/viewer/2022103101/5697bfc61a28abf838ca7166/html5/thumbnails/10.jpg)
Important corollary
What happens if the body of the code doesn't change the condition?if (true) { document.write ("foo"); }
while (true) { document.write ("foo"); }
![Page 11: Loops Robin Burke IT 130. Outline Announcement: Homework #6 Conditionals (review) Iteration while loop while with counter for loops](https://reader037.vdocuments.net/reader037/viewer/2022103101/5697bfc61a28abf838ca7166/html5/thumbnails/11.jpg)
Example: Roll until Doubles
roll.html revising roll.html
![Page 12: Loops Robin Burke IT 130. Outline Announcement: Homework #6 Conditionals (review) Iteration while loop while with counter for loops](https://reader037.vdocuments.net/reader037/viewer/2022103101/5697bfc61a28abf838ca7166/html5/thumbnails/12.jpg)
Note
Use of text area Continuous addition of text to text area
![Page 13: Loops Robin Burke IT 130. Outline Announcement: Homework #6 Conditionals (review) Iteration while loop while with counter for loops](https://reader037.vdocuments.net/reader037/viewer/2022103101/5697bfc61a28abf838ca7166/html5/thumbnails/13.jpg)
Example: Russian Peasant Method How to multiply without knowing the times tables Use
doubling halving addition
Idea two numbers m and n if m is even, we replace m with m/2 and n with n * 2 if m is odd, we write down the value of n in a separate
column (residuals), and replace m with m-1 repeat until m = 1 result is n + sum of residuals
![Page 14: Loops Robin Burke IT 130. Outline Announcement: Homework #6 Conditionals (review) Iteration while loop while with counter for loops](https://reader037.vdocuments.net/reader037/viewer/2022103101/5697bfc61a28abf838ca7166/html5/thumbnails/14.jpg)
Algorithm
turn this idea into something the computer can do
what will be the pieces?
![Page 15: Loops Robin Burke IT 130. Outline Announcement: Homework #6 Conditionals (review) Iteration while loop while with counter for loops](https://reader037.vdocuments.net/reader037/viewer/2022103101/5697bfc61a28abf838ca7166/html5/thumbnails/15.jpg)
Implementation
![Page 16: Loops Robin Burke IT 130. Outline Announcement: Homework #6 Conditionals (review) Iteration while loop while with counter for loops](https://reader037.vdocuments.net/reader037/viewer/2022103101/5697bfc61a28abf838ca7166/html5/thumbnails/16.jpg)
Counter-Driven Loops
Often we want to repeat an action some number of times roll the dice 1000 times print out the first 10 lines of a file
we need a loop that executes some number of times
![Page 17: Loops Robin Burke IT 130. Outline Announcement: Homework #6 Conditionals (review) Iteration while loop while with counter for loops](https://reader037.vdocuments.net/reader037/viewer/2022103101/5697bfc61a28abf838ca7166/html5/thumbnails/17.jpg)
Counter + while loop
To execute body N timesvar counter = 0;while (count < N){
bodycounter = counter + 1;
}
Note counter is only used to keep track of the
repetitions what happens if we don't increment the counter? why isn't the test count <= N or count == N?
![Page 18: Loops Robin Burke IT 130. Outline Announcement: Homework #6 Conditionals (review) Iteration while loop while with counter for loops](https://reader037.vdocuments.net/reader037/viewer/2022103101/5697bfc61a28abf838ca7166/html5/thumbnails/18.jpg)
Alternate formulation
Count down instead of upvar counter = N;
while (count > 0)
{
body
counter = counter - 1;
}
Points is this the right test?
![Page 19: Loops Robin Burke IT 130. Outline Announcement: Homework #6 Conditionals (review) Iteration while loop while with counter for loops](https://reader037.vdocuments.net/reader037/viewer/2022103101/5697bfc61a28abf838ca7166/html5/thumbnails/19.jpg)
Examples
stats.html
![Page 20: Loops Robin Burke IT 130. Outline Announcement: Homework #6 Conditionals (review) Iteration while loop while with counter for loops](https://reader037.vdocuments.net/reader037/viewer/2022103101/5697bfc61a28abf838ca7166/html5/thumbnails/20.jpg)
Exercise
Write a function StringGen takes a short string s and a number n returns a string containing the s repeated n times
Example StringGen ("a", 4) returns "aaaa" StringGen ("la", 3) returns "lalala"
![Page 21: Loops Robin Burke IT 130. Outline Announcement: Homework #6 Conditionals (review) Iteration while loop while with counter for loops](https://reader037.vdocuments.net/reader037/viewer/2022103101/5697bfc61a28abf838ca7166/html5/thumbnails/21.jpg)
Variant
What if we wanted the resulting string to be no longer that a certain
size length of a string given by var.length
if var is a string variable
Example StringGenMax ("ab", 20, 15)
Write just the condition part
![Page 22: Loops Robin Burke IT 130. Outline Announcement: Homework #6 Conditionals (review) Iteration while loop while with counter for loops](https://reader037.vdocuments.net/reader037/viewer/2022103101/5697bfc61a28abf838ca7166/html5/thumbnails/22.jpg)
For loops
Simplifies the counter-driven pattern To execute body N times
var counter = 0;while (count < N){
bodycounter = counter + 1;
}
For-loop versionfor (counter = 0; counter < N; counter = counter + 1){
body}
![Page 23: Loops Robin Burke IT 130. Outline Announcement: Homework #6 Conditionals (review) Iteration while loop while with counter for loops](https://reader037.vdocuments.net/reader037/viewer/2022103101/5697bfc61a28abf838ca7166/html5/thumbnails/23.jpg)
For syntax
for (variable = initial value; exit condition; increment step) fairly flexible but almost always used for simple countingfor (i = 0; i < N; i++)
{
body}
Note repeats the body N times i is a conventional name for a loop counter i++ is the same as i = i + 1
![Page 24: Loops Robin Burke IT 130. Outline Announcement: Homework #6 Conditionals (review) Iteration while loop while with counter for loops](https://reader037.vdocuments.net/reader037/viewer/2022103101/5697bfc61a28abf838ca7166/html5/thumbnails/24.jpg)
Special case
the for loop is a special case of the while loop you can always rewrite a for loop as a while loopfor (variable = initial value; condition; increment step){
body}
Rewritten asvariable = initial value;while (condition){
bodyincrement step;
}
![Page 25: Loops Robin Burke IT 130. Outline Announcement: Homework #6 Conditionals (review) Iteration while loop while with counter for loops](https://reader037.vdocuments.net/reader037/viewer/2022103101/5697bfc61a28abf838ca7166/html5/thumbnails/25.jpg)
Example
for version of stats.html
![Page 26: Loops Robin Burke IT 130. Outline Announcement: Homework #6 Conditionals (review) Iteration while loop while with counter for loops](https://reader037.vdocuments.net/reader037/viewer/2022103101/5697bfc61a28abf838ca7166/html5/thumbnails/26.jpg)
Homework #7
Exercises 13.15 and 13.18 from the book Put web pages on the server
![Page 27: Loops Robin Burke IT 130. Outline Announcement: Homework #6 Conditionals (review) Iteration while loop while with counter for loops](https://reader037.vdocuments.net/reader037/viewer/2022103101/5697bfc61a28abf838ca7166/html5/thumbnails/27.jpg)
Monday
Arrays, Chapter 17