Loops Robin Burke IT 130. Outline Announcement: Homework #6 Conditionals (review) Iteration while loop while with counter for loops.

Slides:



Advertisements
Similar presentations
Repetition control structures
Advertisements

Loops (Part 1) Computer Science Erwin High School Fall 2014.
Computer programming Lecture 3. Lecture 3: Outline Program Looping [Kochan – chap.5] –The for Statement –Relational Operators –Nested for Loops –Increment.
Executes a statement or statements for a number of times – iteration. Syntax for(initialize; test; increment) { // statements to be executed } Initial.
Computer Science 1620 Loops.
Program Design and Development
Chapter 5: Control Structures II (Repetition)
Loops – While, Do, For Repetition Statements Introduction to Arrays
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Loops II Lecture 13, Thu Feb
CS 117 Spring 2002 Repetition Hanly Chapter 4 Friedman-Koffman Chapter 5.
Computer Programming 1 Repetition. Computer Programming 2 Objectives Repetition structures Study while and do loops Examine for loops A practical example.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
11 Chapter 4 LOOPS AND FILES. 22 THE INCREMENT AND DECREMENT OPERATORS To increment a variable means to increase its value by one. To decrement a variable.
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
Invitation to Computer Science, Java Version, Second Edition.
Lecture Set 5 Control Structures Part D - Repetition with 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.
Chapter 5 Control Structures: Loops 5.1 The while Loop The while loop is probably the most frequently used loop construct. The while loop is a conditional.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
PHP Programming with MySQL Slide 4-1 CHAPTER 4 Functions and Control Structures.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
Control Structures II (Repetition). Objectives In this chapter you will: Learn about repetition (looping) control structures Explore how to construct.
ㅎㅎ logical operator if if else switch while do while for Third step for Learning C++ Programming Repetition Control Structures.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
Visual Basic Programming
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
L OO P S While writing a program, there may be a situation when you need to perform some action over and over again. In such situation you would need.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 ‏ Control Structures.
XP Tutorial 10New Perspectives on HTML and XHTML, Comprehensive 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties Tutorial.
Repetition Structures Repetition Structures allow you to write programs that will repeat program steps multiple times. –Also called Loops –Counter controlled.
Algorithm Design.
JavaScript, Fourth Edition
1 Chapter 9. To familiarize you with  Simple PERFORM  How PERFORM statements are used for iteration  Options available with PERFORM 2.
 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.
1 A Balanced Introduction to Computer Science, 2/E David Reed, Creighton University ©2008 Pearson Prentice Hall ISBN Chapter 13 Conditional.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
IS2802 Introduction to Multimedia Applications for Business Lecture 4: JavaScript, Loops, and Conditional Statements Rob Gleasure
A Balanced Introduction to Computer Science, 3/E David Reed, Creighton University ©2011 Pearson Prentice Hall ISBN Chapter 13 Conditional.
COMP Loop Statements Yi Hong May 21, 2015.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
Fourth Quarter.  Involves loops or cycles ◦ Loops: means that a process may be repeated as long as certain condition remains true or remains false. ◦
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
JavaScript, Sixth Edition
Expressions and Data Types Professor Robin Burke.
Arrays and Loops. Learning Objectives By the end of this lecture, you should be able to: – Understand what a loop is – Appreciate the need for loops and.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
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.
REPETITION CONTROL STRUCTURE
Lecture 6 Repetition Richard Gesick.
Warm-up Program Use the same method as your first fortune cookie project and write a program that reads in a string from the user and, at random, will.
Programming the Web using XHTML and JavaScript
3rd prep. – 2nd Term MOE Book Questions.
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
LESSON 11 – WHILE LOOPS UNIT 5 – 1/10/17.
CPS120: Introduction to Computer Science
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Outline Altering flow of control Boolean expressions
Chapter (3) - Looping Questions.
T. Jumana Abu Shmais – AOU - Riyadh
Iteration: Beyond the Basic PERFORM
The structure of programming
FLUENCY WITH INFORMATION TECNOLOGY
The structure of programming
Thinking procedurally
Chapter 13 Conditional Repetition
Presentation transcript:

Loops Robin Burke IT 130

Outline Announcement: Homework #6 Conditionals (review) Iteration while loop while with counter for loops

Homework #6 Report will be part of portfolio should be submitted by both partners put an electronic version on the web server But revision only of the report don't re-design pages don't re-do experiments You will get a detailed description of the portfolio assignment as it gets closer

If statements Control of sequence of execution boolean expression value of expression controls what subsequent code is executed Two forms if (condition) { then part } if (condition) { then part } else { else part }

Iteration Instructions on a shampoo bottle put on hair lather rinse repeat We call this "iteration" executing some action repeatedly usually not forever, but according to some algorithm

Examples Roll the dice until you make your point or get a 7 Calculate a grade for each student in the class Examine each word in a document, looking for one that is misspelled

JavaScript constructs while loop for loop

while Syntax while (condition) {... body... } Meaning if the condition is true, execute the body if the condition is still true, do it again etc. if the condition ever becomes false, stop

Comparison of if and while Appearance is similar if (condition) {... body... } while (condition) {... body... } Meaning is similar true condition means body is executed Difference is in repetition body in if statement is executed at most once body in while loop is repeatedly executed until condition is false

Important corollary What happens if the body of the code doesn't change the condition? if (true) { document.write ("foo"); } while (true) { document.write ("foo"); }

Example: Roll until Doubles roll.html revising roll.html

Note Use of text area Continuous addition of text to text area

Example: Russian Peasant Method How to multiply without knowing the times tables Use doubling halving addition Idea two numbers m and n if m is even, we replace m with m/2 and n with n * 2 if m is odd, we write down the value of n in a separate column (residuals), and replace m with m-1 repeat until m = 1 result is n + sum of residuals

Algorithm turn this idea into something the computer can do what will be the pieces?

Implementation

Counter-Driven Loops Often we want to repeat an action some number of times roll the dice 1000 times print out the first 10 lines of a file we need a loop that executes some number of times

Counter + while loop To execute body N times var counter = 0; while (count < N) { body counter = counter + 1; } Note counter is only used to keep track of the repetitions what happens if we don't increment the counter? why isn't the test count <= N or count == N?

Alternate formulation Count down instead of up var counter = N; while (count > 0) { body counter = counter - 1; } Points is this the right test?

Examples stats.html

Exercise Write a function StringGen takes a short string s and a number n returns a string containing the s repeated n times Example StringGen ("a", 4) returns "aaaa" StringGen ("la", 3) returns "lalala"

Variant What if we wanted the resulting string to be no longer that a certain size length of a string given by var.length if var is a string variable Example StringGenMax ("ab", 20, 15) Write just the condition part

For loops Simplifies the counter-driven pattern To execute body N times var counter = 0; while (count < N) { body counter = counter + 1; } For-loop version for (counter = 0; counter < N; counter = counter + 1) { body }

For syntax for (variable = initial value; exit condition; increment step) fairly flexible but almost always used for simple counting for (i = 0; i < N; i++) { body } Note repeats the body N times i is a conventional name for a loop counter i++ is the same as i = i + 1

Special case the for loop is a special case of the while loop you can always rewrite a for loop as a while loop for (variable = initial value; condition; increment step) { body } Rewritten as variable = initial value; while (condition) { body increment step; }

Example for version of stats.html

Homework #7 Exercises and from the book Put web pages on the server

Monday Arrays, Chapter 17