Java Script VI Loops Repetition Statements Iteration l

  • Slides: 24
Download presentation
Java. Script VI Loops & Repetition Statements

Java. Script VI Loops & Repetition Statements

Iteration l Instructions on a shampoo bottle l l l put on hair lather

Iteration l Instructions on a shampoo bottle l l l put on hair lather rinse repeat We call this "iteration" l l executing some action repeatedly usually not forever, but according to some algorithm

Examples l l Roll the dice until you make your roll doubles Calculate a

Examples l l Roll the dice until you make your roll doubles Calculate a grade for each student in the class Scan each word in a document, looking for one that is misspelled Compute the monthly interest on the loan for each of the next 12 months

Java. Script constructs l while loop l l Used to repeatedly perform a sequence

Java. Script constructs l while loop l l Used to repeatedly perform a sequence of statements as long as some condition holds for loop l Used to repeatedly perform a sequence of statements for a specified number of times

while l Syntax Meaning: … while (condition) {. . . body. . . }

while l Syntax Meaning: … while (condition) {. . . body. . . } … Upon reaching the while, check the condition. Execute the body of the loop repeatedly as long as the condition remains true. If and when the condition becomes false, then exit the loop and continue with the rest of the program Note that the body of the loop must change the condition. Why?

Comparison of if and while l Appearance is similar if (condition) {. . .

Comparison of if and while l Appearance is similar if (condition) {. . . body. . . } while (condition) {. . . body. . . } l l Meaning is similar l true condition means body is executed Difference is in repetition l body in if statement is executed at most once l body in while loop is repeatedly executed until condition is false

Example l Get a positive number N as value from user, compute and print

Example l Get a positive number N as value from user, compute and print the sum from 1 to N N = prompt(“Enter a number: ”, “ 0”); N = parse. Int(N); sum = 0; i = 1; while (i <= N) { sum = sum + i What if we forgot this step? i = i + 1; } document. write(“Sum of numbers from 1 to “ + N + “ is: “ + sum + “. ”); l Exercise: modify this to compute the sum of all numbers between two user-specified number M and N, with M < N.

While Loop Example l example: roll two dice repeatedly until doubles are obtained sample

While Loop Example l example: roll two dice repeatedly until doubles are obtained sample output: note: even though while loops and if statements look similar, they are very different control statements n n an if statement may execute its code 1 time or not at all a while loop may execute its code an arbitrary number of times (including not at all) 8

While Loop Page 9

While Loop Page 9

Counter-Driven Loops l l The Sum of 1 to N program was an example

Counter-Driven Loops l l The Sum of 1 to N program was an example of a “counter-driven” loop Often we want to repeat an action some number of times l l l roll the dice 1000 times print out the first 10 lines of a file we need l a loop that executes some number of times

Counter + while loop l General form to execute body N times var counter

Counter + while loop l General form to execute body N times var counter = 0; while (count < N) { body counter = counter + 1; } l Note l l l 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?

Counter-Driven Loops examples: 12

Counter-Driven Loops examples: 12

Counter-Driven Loops Page 13

Counter-Driven Loops Page 13

Common Errors Body of the code doesn't change the condition i = 1; while

Common Errors Body of the code doesn't change the condition i = 1; while (i <= N) { sum = sum + i } Wrong initialization i = 0; while (i <= N) { sum = sum + i i = i + 1; } Modification step is out of order i = 1; while (i <= N) { i = i + 1; sum = sum + i }

More Common Errors Wrong condition How about this? i = 1; while (i <

More Common Errors Wrong condition How about this? i = 1; while (i < N) { sum = sum + i } // print odd numbers < 10 x = 1; while (x != 10) { document. writeln(x) ; x = x + 2; }

An Alternate Formulation l Count down instead of up var counter = N; while

An Alternate Formulation l Count down instead of up var counter = N; while (count > 0) { body counter = counter - 1; } l Points l is the right test?

Example l count. html l Purpose: Count down to 0 l Some new features:

Example l count. html l Purpose: Count down to 0 l Some new features: l Continuous addition of text to text area document. Count. Form. Output. value = document. Count. Form. Output. value + count + "n";

Countdown Page

Countdown Page

Loops Without Counters l Loop conditions can be based on any Boolean expression l

Loops Without Counters l Loop conditions can be based on any Boolean expression l l l Not just involving counters Such as comparison of two variables or quantities Example: roll. html l Purpose: keep rolling until doubles l Again using continuous addition of text to text area document. Dice. Form. Output. value = document. Dice. Form. Output. value + "You rolled: " + roll 1 + " " + roll 2 + "n";

Other Examples l stats. html

Other Examples l stats. html

For loops l l Simplifies the counter-driven pattern To execute body N times var

For loops l l Simplifies the counter-driven pattern To execute body N times var counter = 0; while (count < N) { body counter = counter + 1; } l For-loop version for (counter = 0; counter < N; counter = counter + 1) { body }

For syntax l for (variable = initial value; exit condition; increment step) l l

For syntax l for (variable = initial value; exit condition; increment step) l l fairly flexible but almost always used for simple counting for (i = 0; i < N; i++) { body } l Note l l l repeats the body N times i is a conventional name for a loop counter i++ is the same as i = i + 1

Special case l the for loop is a special case of the while loop

Special case l the for loop is a special case of the while loop l you can always rewrite a for loop as a while loop for (variable = initial value; condition; increment step) { body } l Rewritten as variable = initial value; while (condition) { body increment step; }

Example l “For” version of Sum-1 -to-N program N = prompt(“Enter a N =

Example l “For” version of Sum-1 -to-N program N = prompt(“Enter a N = parse. Int(N); sum = 0; for (i = 1; i <= N; sum = sum + i; } document. write(“Sum N number: ”, “ 0”); i++) { of numbers from 1 to “ + + “ is: “ + sum + “. ”);