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.

Slides:



Advertisements
Similar presentations
Outline IS400: Development of Business Applications on the Internet Fall 2004 Instructor: Dr. Boris Jukic JavaScript: Arrays.
Advertisements

Programming with Microsoft Visual Basic th Edition
Week 5: Loops 1.  Repetition is the ability to do something over and over again  With repetition in the mix, we can solve practically any problem that.
Programming Logic and Design Sixth Edition
Programming Logic and Design, Third Edition Comprehensive
Repeating Actions While and For Loops
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.
Chapter 5: Control Structures II (Repetition)
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
CS 106 Introduction to Computer Science I 10 / 04 / 2006 Instructor: Michael Eckmann.
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
An Object-Oriented Approach to Programming Logic and Design Chapter 7 Arrays.
Chapter 10Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Announcements/Reminders l Project 6 due on Thursday March 31 l Exam.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
Introduction to JavaScript for Python Programmers
REPETITION STRUCTURES. Topics Introduction to Repetition Structures The while Loop: a Condition- Controlled Loop The for Loop: a Count-Controlled Loop.
 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.
REVIEW 2 Exam History of Computers 1. CPU stands for _______________________. a. Counter productive units b. Central processing unit c. Copper.
Announcements Assignment 9 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 Day : Functions.
Week 5 - Wednesday.  What did we talk about last time?  Exam 1!  And before that?  Review!  And before that?  if and switch statements.
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.
Programming with C# Iteration LECTURE 3. Summary of last lecture SequenceSelectionif and switch statementsCastingRandom.
CPS120 Introduction to Computer Science Iteration (Looping)
Arrays Tonga Institute of Higher Education. Introduction An array is a data structure Definitions  Cell/Element – A box in which you can enter a piece.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
Arrays 1 Multiple values per variable. Why arrays? Can you collect one value from the user? How about two? Twenty? Two hundred? How about… I need to collect.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 5 Arrays.
6 Chapter 61 Looping Programming Logic and Design, Second Edition, Comprehensive 6.
Control Structures II (Repetition). Objectives In this chapter you will: Learn about repetition (looping) control structures Explore how to construct.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Chapter 4 Grouping Objects. Flexible Sized Collections  When writing a program, we often need to be able to group objects into collections  It is typical.
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.
I Power Higher Computing Software Development High Level Language Constructs.
Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 9 & 10 Repetition Statements.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 8 Arrays.
8-1 Compilers Compiler A program that translates a high-level language program into machine code High-level languages provide a richer set of instructions.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 10 - JavaScript/JScript: Control Structures II Outline 10.1Introduction 10.2Essentials of.
Loops Robin Burke IT 130. Outline Announcement: Homework #6 Conditionals (review) Iteration while loop while with counter for loops.
CPS120 Introduction to Computer Science Iteration (Looping)
ActionScript: For Loops, While Loops, Concatenation and Arrays MMP 220 Multimedia Programming This material was prepared for students in MMP220 Multimedia.
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.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 5 Repetition Structures.
Georgia Institute of Technology Speed part 4 Barb Ericson Georgia Institute of Technology May 2006.
IS2802 Introduction to Multimedia Applications for Business Lecture 4: JavaScript, Loops, and Conditional Statements Rob Gleasure
Unit 6 Repetition Processing Instructor: Brent Presley.
COMP Loop Statements Yi Hong May 21, 2015.
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.
Programming Logic and Design Fifth Edition, Comprehensive Chapter 6 Arrays.
JavaScript, Sixth Edition
Computer Program Flow Control structures determine the order of instruction execution: 1. sequential, where instructions are executed in order 2. conditional,
For Loops & Arrays. my_rect_pink_mc.width = 40 my_rect_pink_mc.alpha =.5 trace my_rect_pink_mc.x= 20 my_rect_pink_mc.y= 20 for (var i:int = 0; i < 250;
Loops and Arrays Chapter 19 and Material Adapted from Fluency Text book.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Learning Javascript From Mr Saem
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
Topics Introduction to Repetition Structures
Programming the Web using XHTML and JavaScript
Chapter 19 and Material Adapted from Fluency Text book
Arrays, For loop While loop Do while loop
T. Jumana Abu Shmais – AOU - Riyadh
FLUENCY WITH INFORMATION TECNOLOGY
Presentation transcript:

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 Day : In solving programming problems, look for analogous situations & morph that solution

Once Is Not Enough Repeating instructions is the source of great power in computing © 2004, Lawrence Snyder

Iteration “Iteration” is another term for “repeat” Iteration doesn’t suffer from the question of whether the first item is counted … in iteration it always is. (Use “repeat” and “iterate” interchangeably unless it matters.) Iterating is usually called “looping” in programming Programming languages have many kinds of statements to help program loops In JS we will use the for -statement

Anatomy of for The for -statement syntax for ( ; ; ) { } -- gives iteration variable its first value -- this test is performed before starting each cycle of loop; if false, quit -- the change to the iteration variable after each cycle for ’s 3 control specifications -- the “control trio”-- are connected by an iteration variable

An Iteration Iterations can count... Test Page var i, text = ""; // Initialize text to empty string for (i=1; i<=5; i=i+1) { text = text + "Iteration no.: " + i + "\n"; } alert(text); Test Page var i, text = ""; // Initialize text to empty string for (i=1; i<=5; i=i+1) { text = text + "Iteration no.: " + i + "\n"; } alert(text); Newline in JS

Iterations Control Actions Iterations can replicate other things... Test Page var i, text="It’s funny!"; for (i=1; i<=3; i=i+1) { text = text + " Ha!"; } alert(text); Test Page var i, text="It’s funny!"; for (i=1; i<=3; i=i+1) { text = text + " Ha!"; } alert(text); It is possible to make it a lot funnier by changing the limit variable to, say, i<=1000

Key Points of Loops The most important features of loops: The starting value of the iteration variable The ending value of the iteration variable The amount the iteration variable changes  As explained in the book, it is possible to completely control these features by properly setting the “control trio,” but programmers have gotten in the habit of writing a single kind of iteration: WFI

World Famous Iteration To loop n times the WFI has this form for ( i=0; i<n; i++) { } Advantages: Fast to type The number of iterations is the number after < 0-origin makes it handy for most computations WFI starts at 0, steps by 1, stops (before) n 0, 1, 2, …, n-1 Same as i=i+1

“Off By 1” Error The most common error when working with iterations is to miscount by 1 Everyone makes this mistake A common place where the “off by 1” error matters is in how many times a loop loops The importance of the WFI is it tells exactly for ( i=0; i<n; i++) { } Number of iterations

Using Iteration In JS Print out a row of things Test Page var i; for (i=0; i<5; i++) { document.write('[ ' + i + ' ]'); } Test Page var i; for (i=0; i<5; i++) { document.write('[ ' + i + ' ]'); }

Doubly Nested Loop A loop within a loop repeats repetitions Test Page var i, j; for (j=0; j<3; j++) { for (i=0; i<5; i++) { document.write('[ ' + j + ',' + i + ' ]'); } Test Page var i, j; for (j=0; j<3; j++) { for (i=0; i<5; i++) { document.write('[ ' + j + ',' + i + ' ]'); } The new code is shown in white

Arrays and Indexes We know about names with multiple instances: Rocky 3, QE 2, John Paul 2 The number is called the name’s index The least index is called the index origin In programming, variables that can be indexed are called arrays Declare arrays in JavaScript: var = new Array ( ); JavaScript arrays are 0-origin Reference array elements w/ brackets: A[0]

Arrays and Loops Loops and arrays work together Declare an array and initialize elements to 8 var j, A = new Array(5); for (j=0; j<5; j++) { A[j] = 8; } WFI and array’s indices both start at 0 Notice what would change to have 1000 elements -- arrays and loops give power Five elements: A[0], A[1], A[2], A[3] & A[4]

Summary Iteration is very powerful because a small amount of code specifies a lot of computation for gives full range of looping limits, steps Use any form of for that works, but using the WFI is a good habit to adopt In a doubly nested loop one iteration has another iteration as its Arrays are variables with many elements that are referred to by their index