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.

Slides:



Advertisements
Similar presentations
Repetition Statements Perform the same task repeatedly Allow the computer to do the tedious, boring things.
Advertisements

While loops.
CS 101 Introductory Programming - Lecture 7: Loops In C & Good Coding Practices Presenter: Ankur Chattopadhyay.
Chapter 20 Iteration Principles. Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Learning Objectives Trace the execution.
Chapter 20 Iteration Principles. Learning Objectives Trace the execution of a given for loop Write a World-Famous Iteration for loop Discuss the structure.
CHAPTER 5: Repetition Control Structure. Objectives  To develop algorithms that use DOWHILE and REPEAT.. UNTIL structures  Introduce a pseudocode for.
CS0004: Introduction to Programming Repetition – Do Loops.
Session 5 JavaScript/JScript: Control Structures II Matakuliah: M0114/Web Based Programming Tahun: 2005 Versi: 5.
LOOP / REPETITION while loop. for loop do/while loop We assume that loops are not meant to be infinite. That is, there should always be a way out of the.
Executes a statement or statements for a number of times – iteration. Syntax for(initialize; test; increment) { // statements to be executed } Initial.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Iteration Principles Once is Not Enough lawrence snyder c h a p t e r 21.
Loops – While, Do, For Repetition Statements Introduction to Arrays
Looping Yong Choi School of Business CSU, Bakersfield.
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
An Object-Oriented Approach to Programming Logic and Design Chapter 6 Looping.
Copyright © Texas Education Agency, Computer Programming For Loops.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
Control Structures - Repetition Chapter 5 2 Chapter Topics Why Is Repetition Needed The Repetition Structure Counter Controlled Loops Sentinel Controlled.
 Section Voting is up  Vote in your section by Thursday, 10pm  Assignment: 20 pts for voting  Two labs this week:  Web App Design Lab  Coin Flipping.
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
1 What is a loop? A loop is a repetition control structure that causes a single statement or block to be executed repeatedly Loops.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
CPS120 Introduction to Computer Science Iteration (Looping)
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Repetition Statements.  Often it is necessary to repeat statements many times  Java has two ways of doing this  while statements  for statements.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 6 Looping.
Compunet Corporation1 Programming with Visual Basic.NET While, Do and For – Next Loops Week 5 Tariq Ibn Aziz.
1 Loops. 2 Topics The while Loop Program Versatility Sentinel Values and Priming Reads Checking User Input Using a while Loop Counter-Controlled (Definite)
C++ for Engineers and Scientists, Third Edition1 Objectives In this chapter, you will learn about: Basic loop structures while loops Interactive while.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
Repetition. Control of Flow SEQUENCE SELECTION (if..else, switch…case) REPETITION.
Today’s Announcements Assignment 8 is due Project 2 is due 7/27/04 We will be skipping chapter 22 Test 3 (chapters 18-21) will be on 7/29/04 Tip of the.
JavaScript, Fourth Edition
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 10 - JavaScript/JScript: Control Structures II Outline 10.1Introduction 10.2Essentials of.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
CPS120 Introduction to Computer Science Iteration (Looping)
Announcements CLUE Tutoring Wednesday nights 7-8:30PM MGH extra-credit points for each session you attend from last week through the end of the quarter.
Repetition Statements (Loops) The do while Loop The last iteration structure in C++ is the do while loop. A do while loop repeats a statement or.
1 do-while Statement 2 Do-While Statement Is a looping control structure in which the loop condition is tested after each iteration of the loop. SYNTAX.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
1 Looping Dale/Weems/Headington. 2 KA/JS/P Warning l Save your work often! l In the Khan Academy, JavaScript environment, infinite loops will lock up.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
Chapter 7: Repetition Structure (Loop) Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills.
Catie Welsh February 9,  Friday - No Lab! ◦ Bring questions on Project 2  Lab 3 due on Friday 2.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Loops and Arrays Chapter 19 and Material Adapted from Fluency Text book.
Learning Javascript From Mr Saem
Chapter 5: Looping. Using the while Loop Loop – A structure that allows repeated execution of a block of statements Loop body – A block of statements.
Loops causes program to execute the certain block of code repeatedly until some conditions are satisfied. Suppose you want to execute some code/s 10 times.
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
IST 210: PHP Logic IST 210: Organization of Data IST2101.
Topic : While, For, Do-While Loop Guided By : Branch : Batch :
REPETITION CONTROL STRUCTURE
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
JavaScript: Control Statements I
Chapter 19 and Material Adapted from Fluency Text book
Iteration with While You can say that again.
Outline Altering flow of control Boolean expressions
3 Control Statements:.
Chapter 8: More on the Repetition Structure
Chapter 6: Repetition Statements
FLUENCY WITH INFORMATION TECNOLOGY
PROGRAM FLOWCHART Iteration Statements.
Looping and Repetition
Presentation transcript:

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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