Loops and Arrays Chapter 19 and Material Adapted from Fluency Text book.

Slides:



Advertisements
Similar presentations
Chapter 20 Iteration Principles. Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Learning Objectives Trace the execution.
Advertisements

Chapter 20 Iteration Principles. Learning Objectives Trace the execution of a given for loop Write a World-Famous Iteration for loop Discuss the structure.
Creating PHP Pages Chapter 7 PHP Decisions Making.
Objectives Using functions to organize PHP code
PHP Functions and Control Structures. 2 Defining Functions Functions are groups of statements that you can execute as a single unit Function definitions.
Introduction to Computers and Programming Lecture 9: For Loops New York University.
Working with JavaScript. 2 Objectives Introducing JavaScript Inserting JavaScript into a Web Page File Writing Output to the Web Page Working with Variables.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Chapter 4 Functions and Control Structures PHP Programming with MySQL.
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.
Repetition Statements repeat block of code until a condition is satisfied also called loops Java supports 3 kinds of loops: while statement – repeats a.
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.
JavaScript, Third Edition
Copyright © Texas Education Agency, Computer Programming For Loops.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements II.
TODAY’S LECTURE Review Chapter 2 Go over exercises.
Programming Concepts MIT - AITI. Variables l A variable is a name associated with a piece of data l Variables allow you to store and manipulate data in.
Chapter 20 Iteration Principles. Learning Objectives Trace the execution of a given for loop Write a World-Famous Iteration for loop Discuss the structure.
Programming with App Inventor Computing Institute for K-12 Teachers Summer 2012 Workshop.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
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.
Session 7 JavaScript/Jscript: Arrays Matakuliah: M0114/Web Based Programming Tahun: 2005 Versi: 5.
XP Tutorial 10New Perspectives on Creating Web Pages with HTML, XHTML, and XML 1 Working with JavaScript Creating a Programmable Web Page for North Pole.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
4 1 Array and Hash Variables CGI/Perl Programming By Diane Zak.
PHP Programming with MySQL Slide 4-1 CHAPTER 4 Functions and Control Structures.
CPS120: Introduction to Computer Science Decision Making in Programs.
Chapter 2 Functions and Control Structures PHP Programming with MySQL 2 nd Edition.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
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,
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
Programming 1 DCT 1033 Control Structures I (Selection) if selection statement If..else double selection statement Switch multiple selection statement.
Bordoloi and Bock Control Structures: Iterative Control.
Introduction to Matlab Module #4 Page 1 Introduction to Matlab Module #4 – Programming Topics 1.Programming Basics (fprintf, standard input) 2.Relational.
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
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
ActionScript: For Loops, While Loops, Concatenation and Arrays MMP 220 Multimedia Programming This material was prepared for students in MMP220 Multimedia.
Digital Image Processing Lecture 6: Introduction to M- function Programming.
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.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Fluency with Information Technology Third Edition by Lawrence Snyder Chapter.
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.
CS190/295 Programming in Python for Life Sciences: Lecture 6 Instructor: Xiaohui Xie University of California, Irvine.
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.
JavaScript, Sixth Edition
XP Tutorial 10New Perspectives on HTML, XHTML, and DHTML, Comprehensive 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties.
String and Lists Dr. José M. Reyes Álamo. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
IST 210: PHP Logic IST 210: Organization of Data IST2101.
String and Lists Dr. José M. Reyes Álamo.
Fluency with Information Technology
Overview: Programming Concepts
JavaScript: Control Statements.
Chapter 19 and Material Adapted from Fluency Text book
Arrays, For loop While loop Do while loop
Outline Altering flow of control Boolean expressions
Chapter 8 JavaScript: Control Statements, Part 2
Javascript Arrays.
Computer Science Core Concepts
The structure of programming
Java Programming Loops
Javascript Chapter 19 and 20 5/3/2019.
Programming Basics Review
Chapter 8 JavaScript: Control Statements, Part 2
Presentation transcript:

Loops and Arrays Chapter 19 and Material Adapted from Fluency Text book

Learning Objectives  Trace the execution of a given for loop  Write a World-Famous Iteration for loop  Discuss the structure of nested loops  Explain the use of indexes  List the rules for arrays; describe the syntax of an array reference  Explain the main programming tasks for online animations

Terminology  Repeat  5 repeats, means you may have done it once followed by 5 more times (the repeats!)  Iterate  5 iterations means that you do it 5 times  Iteration means looping through a series of statements to repeat them  In JavaScript, the main iteration statement is the for loop

for Loop Syntax for ( ; ; ) { }  Text that is not in must be given literally  The statement sequence to be repeated is in the

for Loop Syntax for ( ; ; ) { }  is performed for each iteration  Computer completes the whole statement sequence of the before beginning the next iteration

for Loop Syntax for ( ; ; ) { }  Three operations in the parentheses of the for loop control the number of times the loop iterates  Called the control specification

for Loop Syntax for (j = 1; j < = 3; j = j + 1 ) { }  Use an iteration variable  Iteration variables are normal variables and must be declared  This example uses j as the iteration variable

for Loop Syntax for ( ; ; ) { }  sets the iteration variable’s value for the first (if any) iteration of the loop

for Loop Syntax for ( ; ; ) { }  has the same form as the predicate in a conditional statement  If the test is false outcome, the loop terminates and is skipped

for Loop Syntax for ( ; ; ) { }  If has a true outcome, the is performed  When the statements are completed, the operation is performed  changes iteration variable

for Loop Syntax for ( ; ; ) { }  Next iteration starts with the test, performing the same sequence of operations  Iterations proceed until the test has a false outcome, terminating the loop

for Loop

for Sequence

for Example

Continuation/Termination Test  If you can begin an iteration anywhere, you can end it anywhere  The test follows the rules for predicates—the tests in if statements.  The test is any expression resulting in a Boolean value  It must involve the iteration variable

Step-by-Step  also allows considerable freedom  It allows you to specify how big or small the change in the iteration variable  The amount of change is known as the step or step size: j=j+1 j=j+10

Iteration Variable does Math!  iteration variable is often used in computations in the  Important that you focus on the values of the iteration variable during the loops  For example: fact = 1; for ( j = 1; j <= 5; j = j + 1) { fact = fact * j; {

for Loop Practice: Heads/Tails  Let’s use randNum(2) from Chapter 19  It will return 0 (tails) or 1 (heads)  And flip the “coin” 100 times  Use WFI

Conditional Operators >= greater than or equal to > Greater than < less than <= less than or equal to == equal to != not equal to

Indexing  Indexing is the process of creating a sequence of names by associating a base name with a number  Each indexed item is called an element of the base-named sequence  An index is enclosed in [square brackets] in JavaScript

Arrays [1]  In programming, an indexed base name is called an array  Arrays must be declared  In JavaScript, arrays are declared: var = new Array( )  Notice that Array starts with an uppercase “A”

Arrays [2]  Variables either are or are not arrays var week = new Array(7);  week is the identifier being declared,  new Array(7) specifies that the identifier will be an array variable.  number in parentheses gives the number of array elements  To refer to an array’s length, we use.length

Arrays [3]  Rules for arrays in JavaScript:  Arrays are normal variables initialized by new Array( )  in the declaration is just that—the number of array elements  Array indexing begins at 0  Number of elements in an array is its length  Greatest index of an array is − 1 (because the origin is 0)

Arrays [4]  Array reference consists of array name with an index [enclosed in brackets]  Value to which the index evaluates must be less than the array’s length  Example:  var dwarf = new Array(7);  dwarf[0] = "Happy";  dwarf[1] = "Sleepy"; The number in the bracket is called the subscript

WFI and Arrays  0-origin of the WFI is perfect for 0-origin indexing  Easily allows for iterating through all the values of an array

Summary  The basics of for loop iteration. The control part of a for statement is written in parentheses and the is enclosed in curly braces. With each iteration, the entire statement list is performed. The number of iterations is determined by assignments to, and tests of, the iteration variable as specified in the control part.  In the JavaScript for statement, the component is executed first. Then, prior to each iteration, including the first, the predicate is tested. If it is true, the is performed; otherwise, it is skipped, and the for statement terminates. After each iteration, the operation is performed.

Summary  The principles of iteration ensure that every iteration contains a test and that the test is dependent on variables that change in the loop.  The for statement is very flexible. The can begin with any number, the test can stop the loop at any number, and the operation can increment by various amounts upward or downward.

Summary  In indexing, we create a series of names by associating a number with a base name. If we need more names, we count more numbers. Indexed variables are known as arrays in programming. Like ordinary variables, arrays must be declared, but they use the new Array( ) syntax, in which is the number of elements of the array.

Summary  Array elements—referenced by giving the name and a non-negative index in brackets—can be used like ordinary variables. Arrays and iterations can be effectively used together.  Basic concepts of online animation. All animations achieve the appearance of motion by rapidly displaying a series of still frames.