1 looping dale/weems/headington. 2 ka/js/p warning l save your work often! l in the khan academy,...

Post on 17-Jan-2018

224 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

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?

TRANSCRIPT

1

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.

3

A loop is a repetition control structure.

it causes a single statement or block to be executed repeatedly

What is a loop?

4

While StatementSYNTAX

while ( Expression ) { . . // loop body

.}

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

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

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

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

Count-controlled Loopvar count ;

count = 4;

while (count > 0){

println(count);

count -- ;}println("Done");

OUTPUT

count

9

Count-controlled Loopvar count ;

count = 4;

while (count > 0){

println(count);

count -- ;}println("Done");

OUTPUT

count

4

10

Count-controlled Loopvar count ;

count = 4;

while (count > 0) TRUE{

println(count);

count -- ;}println("Done");

OUTPUT

count

4

11

Count-controlled Loopvar count ;

count = 4;

while (count > 0){

println(count);

count -- ;}println("Done");

OUTPUT 4

count

4

12

Count-controlled Loopvar count ;

count = 4;

while (count > 0){

println(count);

count -- ;}println("Done");

OUTPUT 4

count

3

13

Count-controlled Loopvar count ;

count = 4;

while (count > 0) TRUE{

println(count);

count -- ;}println("Done");

OUTPUT 4

count

3

14

Count-controlled Loopvar count ;

count = 4;

while (count > 0){

println(count);

count -- ;}println("Done");

OUTPUT 4

3

count

3

15

Count-controlled Loopvar count ;

count = 4;

while (count > 0){

println(count);

count -- ;}println("Done");

OUTPUT 4

3

count

2

16

Count-controlled Loopvar count ;

count = 4;

while (count > 0) TRUE{

println(count);

count -- ;}println("Done");

OUTPUT 4

3

count

2

17

Count-controlled Loopvar count ;

count = 4;

while (count > 0){

println(count);

count -- ;}println("Done");

OUTPUT 4

3 2

count

2

18

Count-controlled Loopvar count ;

count = 4;

while (count > 0){

println(count);

count -- ;}println("Done");

OUTPUT 4

3 2

count

1

19

Count-controlled Loopvar count ;

count = 4;

while (count > 0) TRUE{

println(count);

count -- ;}println("Done");

OUTPUT 4

3 2

count

1

20

Count-controlled Loopvar count ;

count = 4;

while (count > 0){

println(count);

count -- ;}println("Done");

OUTPUT 4

3 2 1

count

1

21

Count-controlled Loopvar count ;

count = 4;

while (count > 0){

println(count);

count -- ;}println("Done");

OUTPUT 4

3 2 1

count

0

22

Count-controlled Loopvar count ;

count = 4;

while (count > 0) FALSE{

println(count);

count -- ;}println("Done");

OUTPUT 4

3 2 1

count

0

23

Count-controlled Loopvar count ;

count = 4;

while (count > 0){

println(count);

count -- ;}println("Done");

OUTPUT 4

3 2 1 Done

count

0

24

for Statement

25

A Count-Controlled Loop

SYNTAX

for ( initialization ; test expression ; update ) {

0 or more statements to repeat

}

26

The for loop contains

an initialization

an expression to test for continuing

an update to execute after each iteration of the body

27

Example of Repetition

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

println(num + " Potato");}

28

Example of Repetition num

var num;

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

println(num + " Potato");

OUTPUT

?

29

Example of Repetition num

OUTPUT

1

var num;

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

println(num + " Potato");

30

Example of Repetition num

OUTPUT

1

var num;

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

println(num + " Potato");

true

31

Example of Repetition num

var num;

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

println(num + " Potato");

OUTPUT

1

1Potato

32

Example of Repetition num

OUTPUT

2

var num;

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

println(num + " Potato");

1Potato

33

Example of Repetition num

OUTPUT

2

true

1Potato

var num;

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

println(num + " Potato");

34

Example of Repetition num

var num;

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

println(num + " Potato");

OUTPUT

2

1Potato

2Potato

35

Example of Repetition num

OUTPUT

3

var num;

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

println(num + " Potato");

1Potato

2Potato

36

Example of Repetition num

OUTPUT

3

true

1Potato

2Potato

var num;

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

println(num + " Potato");

37

Example of Repetition num

var num;

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

println(num + " Potato");

OUTPUT

3

1Potato

2Potato

3Potato

38

Example of Repetition num

OUTPUT

4

var num;

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

println(num + " Potato");

1Potato

2Potato

3Potato

39

Example of Repetition num

OUTPUT

4

false

1Potato

2Potato

3Potato

var num;

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

println(num + " Potato");

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

41

The output was:

1Potato2Potato3Potato

42

for (var count = 4 ; count > 0 ; count-- ){ println(count);}

println(“Done”);

Count-controlled Loop

OUTPUT: 4321Done

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

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

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

product = product * i;}

For example

top related