looping dale/weems/headington

47
1 Looping Dale/Weems/Headington

Upload: addison-everett

Post on 01-Jan-2016

35 views

Category:

Documents


0 download

DESCRIPTION

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. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Looping Dale/Weems/Headington

1

Looping

Dale/Weems/Headington

Page 2: Looping Dale/Weems/Headington

2

KA/JS/P Warning

Save your work often!

In the Khan Academy, JavaScript environment, infinite loops will lock up the browser.

Page 3: Looping Dale/Weems/Headington

3

A loop is a repetition control structure.

it causes a single statement or block to be executed repeatedly

What is a loop?

Page 4: Looping Dale/Weems/Headington

4

While Statement

SYNTAX

while ( Expression ) {

.

. // loop body

.

}

NOTE: Loop body can be a null statement, or a block.

Page 5: Looping Dale/Weems/Headington

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

bodystatement

Expression

Page 6: Looping Dale/Weems/Headington

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

Page 7: Looping Dale/Weems/Headington

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

Page 8: Looping Dale/Weems/Headington

8

Count-controlled Loop

var count ;

count = 4;

while (count > 0)

{

println(count);

count -- ;}

println("Done");

OUTPUT

count

Page 9: Looping Dale/Weems/Headington

9

Count-controlled Loop

var count ;

count = 4;

while (count > 0)

{

println(count);

count -- ;}

println("Done");

OUTPUT

count

4

Page 10: Looping Dale/Weems/Headington

10

Count-controlled Loop

var count ;

count = 4;

while (count > 0) TRUE

{

println(count);

count -- ;}

println("Done");

OUTPUT

count

4

Page 11: Looping Dale/Weems/Headington

11

Count-controlled Loop

var count ;

count = 4;

while (count > 0)

{

println(count);

count -- ;}

println("Done");

OUTPUT

4

count

4

Page 12: Looping Dale/Weems/Headington

12

Count-controlled Loop

var count ;

count = 4;

while (count > 0)

{

println(count);

count -- ;}

println("Done");

OUTPUT

4

count

3

Page 13: Looping Dale/Weems/Headington

13

Count-controlled Loop

var count ;

count = 4;

while (count > 0) TRUE

{

println(count);

count -- ;}

println("Done");

OUTPUT

4

count

3

Page 14: Looping Dale/Weems/Headington

14

Count-controlled Loop

var count ;

count = 4;

while (count > 0)

{

println(count);

count -- ;}

println("Done");

OUTPUT

4 3

count

3

Page 15: Looping Dale/Weems/Headington

15

Count-controlled Loop

var count ;

count = 4;

while (count > 0)

{

println(count);

count -- ;}

println("Done");

OUTPUT

4 3

count

2

Page 16: Looping Dale/Weems/Headington

16

Count-controlled Loop

var count ;

count = 4;

while (count > 0) TRUE

{

println(count);

count -- ;}

println("Done");

OUTPUT

4 3

count

2

Page 17: Looping Dale/Weems/Headington

17

Count-controlled Loop

var count ;

count = 4;

while (count > 0)

{

println(count);

count -- ;}

println("Done");

OUTPUT

4 3 2

count

2

Page 18: Looping Dale/Weems/Headington

18

Count-controlled Loop

var count ;

count = 4;

while (count > 0)

{

println(count);

count -- ;}

println("Done");

OUTPUT

4 3 2

count

1

Page 19: Looping Dale/Weems/Headington

19

Count-controlled Loop

var count ;

count = 4;

while (count > 0) TRUE

{

println(count);

count -- ;}

println("Done");

OUTPUT

4 3 2

count

1

Page 20: Looping Dale/Weems/Headington

20

Count-controlled Loop

var count ;

count = 4;

while (count > 0)

{

println(count);

count -- ;}

println("Done");

OUTPUT

4 3 2 1

count

1

Page 21: Looping Dale/Weems/Headington

21

Count-controlled Loop

var count ;

count = 4;

while (count > 0)

{

println(count);

count -- ;}

println("Done");

OUTPUT

4 3 2 1

count

0

Page 22: Looping Dale/Weems/Headington

22

Count-controlled Loop

var count ;

count = 4;

while (count > 0) FALSE

{

println(count);

count -- ;}

println("Done");

OUTPUT

4 3 2 1

count

0

Page 23: Looping Dale/Weems/Headington

23

Count-controlled Loop

var count ;

count = 4;

while (count > 0)

{

println(count);

count -- ;}

println("Done");

OUTPUT

4 3 2 1 Done

count

0

Page 24: Looping Dale/Weems/Headington

24

for Statement

Page 25: Looping Dale/Weems/Headington

25

A Count-Controlled Loop

SYNTAX

for ( initialization ; test expression ; update )

{

0 or more statements to repeat

}

Page 26: Looping Dale/Weems/Headington

26

The for loop contains

an initialization

an expression to test for continuing

an update to execute after each iteration of the body

Page 27: Looping Dale/Weems/Headington

27

Example of Repetition

for ( var num = 1 ; num <= 3 ; num++ ) {

println(num + " Potato");

}

Page 28: Looping Dale/Weems/Headington

28

Example of Repetition num

var num;

for ( num = 1 ; num <= 3 ; num++ ) {

println(num + " Potato");

OUTPUT

?

Page 29: Looping Dale/Weems/Headington

29

Example of Repetition num

OUTPUT

1

var num;

for ( num = 1 ; num <= 3 ; num++ ) {

println(num + " Potato");

Page 30: Looping Dale/Weems/Headington

30

Example of Repetition num

OUTPUT

1

var num;

for ( num = 1 ; num <= 3 ; num++ ) {

println(num + " Potato");

true

Page 31: Looping Dale/Weems/Headington

31

Example of Repetition num

var num;

for ( num = 1 ; num <= 3 ; num++ ) {

println(num + " Potato");

OUTPUT

1

1Potato

Page 32: Looping Dale/Weems/Headington

32

Example of Repetition num

OUTPUT

2

var num;

for ( num = 1 ; num <= 3 ; num++ ) {

println(num + " Potato");

1Potato

Page 33: Looping Dale/Weems/Headington

33

Example of Repetition num

OUTPUT

2

true

1Potato

var num;

for ( num = 1 ; num <= 3 ; num++ ) {

println(num + " Potato");

Page 34: Looping Dale/Weems/Headington

34

Example of Repetition num

var num;

for ( num = 1 ; num <= 3 ; num++ ) {

println(num + " Potato");

OUTPUT

2

1Potato

2Potato

Page 35: Looping Dale/Weems/Headington

35

Example of Repetition num

OUTPUT

3

var num;

for ( num = 1 ; num <= 3 ; num++ ) {

println(num + " Potato");

1Potato

2Potato

Page 36: Looping Dale/Weems/Headington

36

Example of Repetition num

OUTPUT

3

true

1Potato

2Potato

var num;

for ( num = 1 ; num <= 3 ; num++ ) {

println(num + " Potato");

Page 37: Looping Dale/Weems/Headington

37

Example of Repetition num

var num;

for ( num = 1 ; num <= 3 ; num++ ) {

println(num + " Potato");

OUTPUT

3

1Potato

2Potato

3Potato

Page 38: Looping Dale/Weems/Headington

38

Example of Repetition num

OUTPUT

4

var num;

for ( num = 1 ; num <= 3 ; num++ ) {

println(num + " Potato");

1Potato

2Potato

3Potato

Page 39: Looping Dale/Weems/Headington

39

Example of Repetition num

OUTPUT

4

false

1Potato

2Potato

3Potato

var num;

for ( num = 1 ; num <= 3 ; num++ ) {

println(num + " Potato");

Page 40: Looping Dale/Weems/Headington

40

Example of Repetition num

When the loop control condition is evaluated and has value false, theloop is said to be “satisfied” and control passes to the statementfollowing the For statement.

4

falsevar num;

for ( num = 1 ; num <= 3 ; num++ ) {

println(num + " Potato");

Page 41: Looping Dale/Weems/Headington

41

The output was:

1Potato2Potato3Potato

Page 42: Looping Dale/Weems/Headington

42

for (var count = 4 ; count > 0 ; count-- )

{

println(count);

}

println(“Done”);

Count-controlled Loop

OUTPUT: 4321Done

Page 43: Looping Dale/Weems/Headington

Count Control Loop Example

Display integers and their squares from 1 through 10.

for (var i = 1; i <= 10; i++) { println(i + " " + i*i);

}

Page 44: Looping Dale/Weems/Headington

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

}

Page 45: Looping Dale/Weems/Headington

For example

Display integers and their squares from 10 down to 1.

for (var i = 10; i >= 1; i--) {println(i + " " + i*i);

}

Page 46: Looping Dale/Weems/Headington

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

}

Page 47: Looping Dale/Weems/Headington

Compute and return n! = 1 2 3 ... n.

var product = 1;

for (var i = 2; i <= n; i++) {

product = product * i;

}

For example