Presentation is loading. Please wait.

Presentation is loading. Please wait.

Concepts of Algorithmic Thinking Iterations, or Loops Once is not Enough D.A. Clements, Lecturer Information School 2/23/2016 D.A. Clements, MLIS, UW Information.

Similar presentations


Presentation on theme: "Concepts of Algorithmic Thinking Iterations, or Loops Once is not Enough D.A. Clements, Lecturer Information School 2/23/2016 D.A. Clements, MLIS, UW Information."— Presentation transcript:

1 Concepts of Algorithmic Thinking Iterations, or Loops Once is not Enough D.A. Clements, Lecturer Information School 2/23/2016 D.A. Clements, MLIS, UW Information School 1

2 Objectives Learn the syntax of loops Use loops to count down or count up Recognize the World-Famous Iteration Learn how to start, increment, and end a loop Describe the structure of nested loops 2/23/2016 D.A. Clements, MLIS, UW Information School 2

3 ITERATION Play it again, Sam. 2/23/2016 D.A. Clements, MLIS, UW Information School 3

4 Definitions Iteration, or looping, is the process of repetition: looping through a sequence of statements to repeat them 2/23/2016 D.A. Clements, MLIS, UW Information School 4

5 Major Types of Iterations For loop 1. Baby 2. Count up 3. Count down While loop 4. Count up 5. Count down 2/23/2016 D.A. Clements, MLIS, UW Information School 5

6 WHILE LOOPS Repetition is good 6 D.A. Clements, MLIS, UW Information School2/23/2016 Reduce, Reuse, Recycle

7 while Loop Basic Syntax <initialization while ) { } The whole sequence of statements in the statement list is performed for each loop Computer completes the whole statement sequence of the before beginning the next loop 7 D.A. Clements, MLIS, UW Information School2/23/2016

8 var num=0; //Declare & initialize counter while (num 10 { alert( num ); // start loop body document.write(" " + num + " "); num++; //increments counter by 1 } How a while Loop Works 8 D.A. Clements, MLIS, UW Information School2/23/2016

9 DO…WHILE LOOPS Repetition is good 9 D.A. Clements, MLIS, UW Information School2/23/2016 Reduce, Reuse, Recycle

10 do…while Loop Basic Syntax for ( ; ; ) { } The whole sequence of statements in the statement list is performed for each loop Computer completes the whole statement sequence of the before beginning the next loop 10 D.A. Clements, MLIS, UW Information School2/23/2016

11 var num=0; //Declare & initialize counter do { alert( num ); // start loop body document.write(" " + num + " "); num++; //increments counter by 1 } while (num 10 How a do…while Loop Works 11 D.A. Clements, MLIS, UW Information School2/23/2016

12 FOR LOOPS Save that code! Use it again! 2/23/2016 D.A. Clements, MLIS, UW Information School 12 Reduce, Reuse, Recycle

13 The for Loop Basic Syntax for ( ; ; ) { } The whole sequence of statements in the statement list is performed for each iteration Computer completes the whole statement sequence of the before beginning the next iteration 2/23/2016 D.A. Clements, MLIS, UW Information School 13

14 Control specification The three operations in the parentheses of the for loop Control the number of times the loop iterates by using an iteration variable (must be declared) 2/23/2016 D.A. Clements, MLIS, UW Information School 14

15 How a for Loop Works Consider a computation on declared variables j and text text = "She said "; for ( var j = 1; j <= 3; j = j + 1 ) { text = text + "Never! "; } alert(text); 2/23/2016 D.A. Clements, MLIS, UW Information School 15

16 How a for Loop Works Consider a computation on declared variables j and text text = "She said "; for ( var j = 1; j <= 3; j = j + 1 ) { text = text + "Never! "; } alert(text); 2/23/2016 D.A. Clements, MLIS, UW Information School 16 Control specification

17 How a for Loop Works Consider a computation on declared variables j and text text = "She said "; for ( var j = 1; j <= 3; j = j + 1 ) { text = text + "Never! "; } alert(text); 2/23/2016 D.A. Clements, MLIS, UW Information School 17 Starting point

18 How a for Loop Works Consider a computation on declared variables j and text text = "She said "; for ( var j = 1; j <= 3; j = j + 1 ) { text = text + "Never! "; } alert(text); 2/23/2016 D.A. Clements, MLIS, UW Information School 18 Continuation condition

19 How a for Loop Works Consider a computation on declared variables j and text text = "She said "; for (var j = 1; j <= 3; j = j + 1 ) { text = text + "Never! "; } alert(text); 2/23/2016 D.A. Clements, MLIS, UW Information School 19 Step size or increment

20 How a for Loop Works Demo: text = “The two-year-old said "; for ( j = 1; j <= 3; j = j + 1 ) { text = text + "No! "; alert(text); } 2/23/2016 D.A. Clements, MLIS, UW Information School 20

21 Processing for loops Example: for ( j = 1 ; j <= 3 ; j = j + 1) { } The first operation is the Sets the iteration variable's value for the first iteration of the loop. Done only once. The next operation is Test. If the test has a false outcome, the is skipped and control passes to the next statement after the for loop If the test has a true outcome, the is performed. When the statements are complete, the operation is performed Repeats with the continuation test, performs same sequence of steps. 2/23/2016 D.A. Clements, MLIS, UW Information School 21

22 The World-Famous Iteration for ( j = 0; j < n; j++ ) {…} Most frequently written for loop of all time Easy to see iteration count: Always n times When n is 3 0 is first loop 1 is second loop 2 is third loop 3 is fourth and it doesn't run. 2/23/2016 D.A. Clements, MLIS, UW Information School 22

23 Running through a for loop 2/23/2016 D.A. Clements, MLIS, UW Information School 23 1 2 3

24 Rules: counter and start point The Iteration Variable: j = 1; Must be declared, and follow rules for variable identifiers i, j, and k are the most common choices The Starting Point Iteration can begin anywhere, including negative numbers 2/23/2016 D.A. Clements, MLIS, UW Information School 24

25 Continuation/Termination Test j <= 3 Test is any expression resulting in a Boolean value (true/false) Continuation must involve iteration variable to avoid infinite loop Step Size j = j + 1 Amount of change from one iteration to the next Often called the increment or decrement Increment: j + 1 Decrement: j - 1 2/23/2016 D.A. Clements, MLIS, UW Information School 25 Rules: Continuation & Step Size

26 Experiments with Flipping Coins Demo…. 2/23/2016 26 D.A. Clements, MLIS, UW Information School

27 Experiments with Flipping Coins Demo…. 2/23/2016 27 D.A. Clements, MLIS, UW Information School

28 Experiments with Flipping Coins Demo…. 2/23/2016 28 D.A. Clements, MLIS, UW Information School

29 Experiments with Flipping Coins Demo…. 2/23/2016 29 0 = 1x 99 = +99x 100 times! D.A. Clements, MLIS, UW Information School

30 Experiments with Flipping Coins Demo…. 2/23/2016 30 D.A. Clements, MLIS, UW Information School

31 Demo—100 coin tosses Try the Coin Toss Example 6 in Module 6 of our course Web site 2/23/2016 D.A. Clements, MLIS, UW Information School 31

32 Nested for Loop Basic Syntax for ( ; ; ) { for ( ; ; ) { } 2/23/2016 D.A. Clements, MLIS, UW Information School 32

33 Experiment 2—with Five Trials A Nested Loop To run several trials, consider the entire loop we just looked at as one Trial Create another for loop containing this Trial unit, adding a couple of needed statements We have a loop within a loop (nested loop) which causes the Trial loop (0-99) to run five times 2/23/2016 D.A. Clements, MLIS, UW Information School 33

34 Experiment 2—the original trial Demo… 2/23/2016 D.A. Clements, MLIS, UW Information School 34

35 Experiment 2—outer loop Demo… 2/23/2016 D.A. Clements, MLIS, UW Information School 35

36 Experiment 2—declare i and j Demo… 2/23/2016 D.A. Clements, MLIS, UW Information School 36

37 Experiment 2—set heads, tails to zero Demo… 2/23/2016 D.A. Clements, MLIS, UW Information School 37

38 Experiment 2—how far from 50%? Demo… 2/23/2016 D.A. Clements, MLIS, UW Information School 38

39 Demo—Five Trials Try the Five-Trial Coin Toss Example 7 in Module 6 of our course Web site 2/23/2016 D.A. Clements, MLIS, UW Information School 39

40 Summary Learn the syntax of loops Use loops to count down or count up Recognize the World-Famous Iteration Learn how to start, increment, and end a loop Describe the structure of nested loops 2/23/2016 D.A. Clements, MLIS, UW Information School 40

41 Quiz topics for next week For loops 2/23/2016 41 D.A. Clements, MLIS, UW Information School

42 End papers… A computer lets you make more mistakes faster than any invention in human history—with the possible exceptions of handguns and tequila. ~Mitche Ratcliffe 2/23/2016 D.A. Clements, MLIS, UW Information School 42


Download ppt "Concepts of Algorithmic Thinking Iterations, or Loops Once is not Enough D.A. Clements, Lecturer Information School 2/23/2016 D.A. Clements, MLIS, UW Information."

Similar presentations


Ads by Google