Download presentation
Presentation is loading. Please wait.
Published byBranden Walker Modified over 9 years ago
1
Loops Robin Burke IT 130
2
Outline Announcement: Homework #6 Conditionals (review) Iteration while loop while with counter for loops
3
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
4
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 }
5
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
6
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
7
JavaScript constructs while loop for loop
8
while Syntax while (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
9
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
10
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"); }
11
Example: Roll until Doubles roll.html revising roll.html
12
Note Use of text area Continuous addition of text to text area
13
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
14
Algorithm turn this idea into something the computer can do what will be the pieces?
15
Implementation
16
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
17
Counter + while loop To execute body N times var counter = 0; while (count < N) { body counter = 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?
18
Alternate formulation Count down instead of up var counter = N; while (count > 0) { body counter = counter - 1; } Points is this the right test?
19
Examples stats.html
20
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"
21
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
22
For loops Simplifies the counter-driven pattern To execute body N times var counter = 0; while (count < N) { body counter = counter + 1; } For-loop version for (counter = 0; counter < N; counter = counter + 1) { body }
23
For syntax for (variable = initial value; exit condition; increment step) fairly flexible but almost always used for simple counting for (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
24
Special case the for loop is a special case of the while loop you can always rewrite a for loop as a while loop for (variable = initial value; condition; increment step) { body } Rewritten as variable = initial value; while (condition) { body increment step; }
25
Example for version of stats.html
26
Homework #7 Exercises 13.15 and 13.18 from the book Put web pages on the server
27
Monday Arrays, Chapter 17
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.