500 likes | 624 Views
Looping Dale/Weems/Headington. KA/JS/P Warning. Save your work often! In the Khan Academy, JavaScript environment, infinite loops will lock up the browser. What is a loop?. A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly.
E N D
Looping Dale/Weems/Headington
KA/JS/P Warning • Save your work often! • In the Khan Academy, JavaScript environment, infinite loops will lock up the browser.
What is a loop? • A loop is a repetition control structure. • it causes a single statement or block to be executed repeatedly
While Statement SYNTAX while ( Expression) { . . // loop body . } NOTE: Loop body can be a null statement, or a block.
When the expression is tested and found to be false, the loop is exited and control passes to the statement which follows the loop body. WHILE LOOP FALSE Expression TRUE body statement
Count-controlled loop contains an initialization of the loop control variable an expression to test for continuing the loop an update of the loop controlvariable to be executed with each iteration of the body
Count-controlled Loop var count ; count = 4; // initialize loop variable while (count > 0) // test expression { println(count); // repeated action count --;// update loop variable } println("Done");
count Count-controlled Loop var count ; count = 4; while (count > 0) { println(count); count --; } println("Done"); OUTPUT
count 4 Count-controlled Loop var count ; count = 4; while (count > 0) { println(count); count --; } println("Done"); OUTPUT
count 4 Count-controlled Loop var count ; count = 4; while (count > 0) TRUE { println(count); count --; } println("Done"); OUTPUT
count 4 Count-controlled Loop var count ; count = 4; while (count > 0) { println(count); count --; } println("Done"); OUTPUT 4
count 3 Count-controlled Loop var count ; count = 4; while (count > 0) { println(count); count --; } println("Done"); OUTPUT 4
count 3 Count-controlled Loop var count ; count = 4; while (count > 0) TRUE { println(count); count --; } println("Done"); OUTPUT 4
count 3 Count-controlled Loop var count ; count = 4; while (count > 0) { println(count); count --; } println("Done"); OUTPUT 4 3
count 2 Count-controlled Loop var count ; count = 4; while (count > 0) { println(count); count --; } println("Done"); OUTPUT 4 3
count 2 Count-controlled Loop var count ; count = 4; while (count > 0) TRUE { println(count); count --; } println("Done"); OUTPUT 4 3
count 2 Count-controlled Loop var count ; count = 4; while (count > 0) { println(count); count --; } println("Done"); OUTPUT 4 3 2
count 1 Count-controlled Loop var count ; count = 4; while (count > 0) { println(count); count --; } println("Done"); OUTPUT 4 3 2
count 1 Count-controlled Loop var count ; count = 4; while (count > 0) TRUE { println(count); count --; } println("Done"); OUTPUT 4 3 2
count 1 Count-controlled Loop var count ; count = 4; while (count > 0) { println(count); count --; } println("Done"); OUTPUT 4 3 2 1
count 0 Count-controlled Loop var count ; count = 4; while (count > 0) { println(count); count --; } println("Done"); OUTPUT 4 3 2 1
count 0 Count-controlled Loop var count ; count = 4; while (count > 0) FALSE { println(count); count --; } println("Done"); OUTPUT 4 3 2 1
count 0 Count-controlled Loop var count ; count = 4; while (count > 0) { println(count); count --; } println("Done"); OUTPUT 4 3 2 1 Done
A Count-Controlled Loop SYNTAX for ( initialization ; test expression; update ) { 0 or more statements to repeat }
The for loop contains an initialization an expression to test for continuing an update to execute after each iteration of the body
Example of Repetition for ( var num = 1 ; num <= 3; num++) { println(num + " Potato"); }
Example of Repetition ? num var num; for ( num = 1 ; num <= 3; num++) println(num + " Potato"); OUTPUT
Example of Repetition 1 num var num; for ( num = 1 ; num <= 3; num++) println(num + " Potato"); OUTPUT
Example of Repetition 1 num true var num; for ( num = 1 ; num <= 3; num++) println(num + " Potato"); OUTPUT
1Potato Example of Repetition 1 num var num; for ( num = 1 ; num <= 3; num++) println(num + " Potato"); OUTPUT
1Potato Example of Repetition 2 num var num; for ( num = 1 ; num <= 3; num++) println(num + " Potato"); OUTPUT
1Potato Example of Repetition 2 num true var num; for ( num = 1 ; num <= 3; num++) println(num + " Potato"); OUTPUT
1Potato 2Potato Example of Repetition 2 num var num; for ( num = 1 ; num <= 3; num++) println(num + " Potato"); OUTPUT
1Potato 2Potato Example of Repetition 3 num var num; for ( num = 1 ; num <= 3; num++) println(num + " Potato"); OUTPUT
1Potato 2Potato Example of Repetition 3 num true var num; for ( num = 1 ; num <= 3; num++) println(num + " Potato"); OUTPUT
1Potato 2Potato 3Potato Example of Repetition 3 num var num; for ( num = 1 ; num <= 3; num++) println(num + " Potato"); OUTPUT
1Potato 2Potato 3Potato Example of Repetition 4 num var num; for ( num = 1 ; num <= 3; num++) println(num + " Potato"); OUTPUT
1Potato 2Potato 3Potato Example of Repetition 4 num false var num; for ( num = 1 ; num <= 3; num++) println(num + " Potato"); OUTPUT
Example of Repetition 4 num false var num; for ( num = 1 ; num <= 3; num++) println(num + " Potato"); When the loop control condition is evaluated and has value false, the loop is said to be “satisfied” and control passes to the statement following the For statement.
The output was: 1Potato 2Potato 3Potato
Count-controlled Loop for (var count = 4 ; count > 0 ; count-- ) { println(count); } println(“Done”); OUTPUT: 4 3 2 1 Done
Count Control Loop Example Display integers and their squares from 1 through 10. for (var i = 1; i <= 10; i++) { println(i + " " + i*i); }
For example Display even integers and their squares from 1 through 10. for (var i = 2; i <= 10; i = i+2) { println(i + " " + i*i); }
For example Display integers and their squares from 10 down to 1. for (var i = 10; i >= 1; i--) { println(i + " " + i*i); }
For example Find square roots of 1.1, 1.2, 1.3, ..., 2.0 for (var x = 1.1; x <= 2.0; x =x+0.1) { println(x + " " + sqrt(x)); }
For example Compute and return n! = 1 · 2 · 3 · ... · n. var product = 1; for (var i = 2; i <= n; i++) { product = product * i; }
Nested Loop Pattern // Initialize outer loop while (Outer loop condition) { : // Initialize inner loop while (Inner loop condition) { // Inner loop processing & update } : // Outer loop update }
To design a nested loop • begin with outer loop • when you get to where the inner loop appears, make it a separate module and come back to its design later • Nested Count Controlled Loops • Use different loop control variables