Download presentation
Presentation is loading. Please wait.
Published byAntonia Mathews Modified over 9 years ago
1
1 Looping Dale/Weems/Headington
2
2 KA/JS/P Warning l Save your work often! l In the Khan Academy, JavaScript environment, infinite loops will lock up the browser.
3
3 l A loop is a repetition control structure. l it causes a single statement or block to be executed repeatedly What is a loop?
4
4 While Statement SYNTAX while ( Expression ) {.. // loop body. } NOTE: Loop body can be a null statement, or a block.
5
5 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 TRUE body statement Expression
6
6 an initialization of the loop control variable an expression to test for continuing the loop an update of the loop control variable to be executed with each iteration of the body Count-controlled loop contains
7
7 var count ; count = 4; // initialize loop variable while (count > 0) // test expression { println(count); // repeated action count -- ; // update loop variable } println("Done"); Count-controlled Loop
8
8 var count ; count = 4; while (count > 0) { println(count); count -- ; } println("Done"); OUTPUT count
9
9 Count-controlled Loop var count ; count = 4; while (count > 0) { println(count); count -- ; } println("Done"); OUTPUT count 4
10
10 Count-controlled Loop var count ; count = 4; while (count > 0) TRUE { println(count); count -- ; } println("Done"); OUTPUT count 4
11
11 Count-controlled Loop var count ; count = 4; while (count > 0) { println(count); count -- ; } println("Done"); OUTPUT 4 count 4
12
12 Count-controlled Loop var count ; count = 4; while (count > 0) { println(count); count -- ; } println("Done"); OUTPUT 4 count 3
13
13 Count-controlled Loop var count ; count = 4; while (count > 0) TRUE { println(count); count -- ; } println("Done"); OUTPUT 4 count 3
14
14 Count-controlled Loop var count ; count = 4; while (count > 0) { println(count); count -- ; } println("Done"); OUTPUT 4 3 count 3
15
15 Count-controlled Loop var count ; count = 4; while (count > 0) { println(count); count -- ; } println("Done"); OUTPUT 4 3 count 2
16
16 Count-controlled Loop var count ; count = 4; while (count > 0) TRUE { println(count); count -- ; } println("Done"); OUTPUT 4 3 count 2
17
17 Count-controlled Loop var count ; count = 4; while (count > 0) { println(count); count -- ; } println("Done"); OUTPUT 4 3 2 count 2
18
18 Count-controlled Loop var count ; count = 4; while (count > 0) { println(count); count -- ; } println("Done"); OUTPUT 4 3 2 count 1
19
19 Count-controlled Loop var count ; count = 4; while (count > 0) TRUE { println(count); count -- ; } println("Done"); OUTPUT 4 3 2 count 1
20
20 Count-controlled Loop var count ; count = 4; while (count > 0) { println(count); count -- ; } println("Done"); OUTPUT 4 3 2 1 count 1
21
21 Count-controlled Loop var count ; count = 4; while (count > 0) { println(count); count -- ; } println("Done"); OUTPUT 4 3 2 1 count 0
22
22 Count-controlled Loop var count ; count = 4; while (count > 0) FALSE { println(count); count -- ; } println("Done"); OUTPUT 4 3 2 1 count 0
23
23 Count-controlled Loop var count ; count = 4; while (count > 0) { println(count); count -- ; } println("Done"); OUTPUT 4 3 2 1 Done count 0
24
24 for Statement
25
25 A Count-Controlled Loop SYNTAX for ( initialization ; test expression ; update ) { 0 or more statements to repeat }
26
26 The for loop contains an initialization an expression to test for continuing an update to execute after each iteration of the body
27
27 Example of Repetition for ( var num = 1 ; num <= 3 ; num++ ) { println(num + " Potato"); }
28
28 Example of Repetition num var num; for ( num = 1 ; num <= 3 ; num++ ) { println(num + " Potato"); OUTPUT ?
29
29 Example of Repetition num OUTPUT 1 var num; for ( num = 1 ; num <= 3 ; num++ ) { println(num + " Potato");
30
30 Example of Repetition num OUTPUT 1 var num; for ( num = 1 ; num <= 3 ; num++ ) { println(num + " Potato"); true
31
31 Example of Repetition num var num; for ( num = 1 ; num <= 3 ; num++ ) { println(num + " Potato"); OUTPUT 1 1Potato
32
32 Example of Repetition num OUTPUT 2 var num; for ( num = 1 ; num <= 3 ; num++ ) { println(num + " Potato"); 1Potato
33
33 Example of Repetition num OUTPUT 2 true 1Potato var num; for ( num = 1 ; num <= 3 ; num++ ) { println(num + " Potato");
34
34 Example of Repetition num var num; for ( num = 1 ; num <= 3 ; num++ ) { println(num + " Potato"); OUTPUT 2 1Potato 2Potato
35
35 Example of Repetition num OUTPUT 3 var num; for ( num = 1 ; num <= 3 ; num++ ) { println(num + " Potato"); 1Potato 2Potato
36
36 Example of Repetition num OUTPUT 3 true 1Potato 2Potato var num; for ( num = 1 ; num <= 3 ; num++ ) { println(num + " Potato");
37
37 Example of Repetition num var num; for ( num = 1 ; num <= 3 ; num++ ) { println(num + " Potato"); OUTPUT 3 1Potato 2Potato 3Potato
38
38 Example of Repetition num OUTPUT 4 var num; for ( num = 1 ; num <= 3 ; num++ ) { println(num + " Potato"); 1Potato 2Potato 3Potato
39
39 Example of Repetition num OUTPUT 4 false 1Potato 2Potato 3Potato var num; for ( num = 1 ; num <= 3 ; num++ ) { println(num + " Potato");
40
40 Example of Repetition num 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. 4 false var num; for ( num = 1 ; num <= 3 ; num++ ) { println(num + " Potato");
41
41 The output was: 1Potato 2Potato 3Potato
42
42 for (var count = 4 ; count > 0 ; count -- ) { println(count); } println(“Done”); Count-controlled Loop OUTPUT: 4 3 2 1 Done
43
Count Control Loop Example Display integers and their squares from 1 through 10. for (var i = 1; i <= 10; i++) { println(i + " " + i*i); }
44
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); }
45
For example Display integers and their squares from 10 down to 1. for (var i = 10; i >= 1; i--) { println(i + " " + i*i); }
46
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)); }
47
Compute and return n! = 1 2 3 ... n. var product = 1; for (var i = 2; i <= n; i++) { product = product * i; } For example
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.