Loops Robin Burke IT 130. Outline Announcement: Homework #6 Conditionals (review) Iteration while...

Preview:

Citation preview

Loops

Robin Burke

IT 130

Outline

Announcement: Homework #6 Conditionals (review) Iteration

while loop while with counter for loops

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

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 }

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

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

JavaScript constructs

while loop for loop

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

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

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"); }

Example: Roll until Doubles

roll.html revising roll.html

Note

Use of text area Continuous addition of text to text area

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

Algorithm

turn this idea into something the computer can do

what will be the pieces?

Implementation

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

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?

Alternate formulation

Count down instead of upvar counter = N;

while (count > 0)

{

body

counter = counter - 1;

}

Points is this the right test?

Examples

stats.html

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"

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

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}

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

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;

}

Example

for version of stats.html

Homework #7

Exercises 13.15 and 13.18 from the book Put web pages on the server

Monday

Arrays, Chapter 17

Recommended