Loops - Monday, Week 4  What are loops?  While loops  Classic loop example - the for loop  Parts of a loop  Examples.

Slides:



Advertisements
Similar presentations
Program Looping EE2372 Software Design I Dr. Gerardo Rosiles.
Advertisements

Looping Structures: Do Loops
CS107 Introduction to Computer Science Lecture 3, 4 An Introduction to Algorithms: Loops.
Tonight’s JavaScript Topics 1 Conditional Statements: if and switch The Array Object Looping Statements: for, while and do-while.
1 PHP Statement Constructs Server Scripting. 5-2 Basic Statement All Statements end in a semicolon. Statements are delimited from the HTML code by enclosing.
ITC 240: Web Application Programming
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
Tutorial 12 Working with Arrays, Loops, and Conditional Statements
Loops – While, Do, For Repetition Statements Introduction to Arrays
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
16/27/ :53 PM6/27/ :53 PM6/27/ :53 PMLogic Control Structures Arithmetic Expressions Used to do arithmetic. Operations consist of +,
CSE 113 Week 5 February , Announcements  Module 2 due 2/15  Exam 3 is on 2/15  Module 3 due 2/22  Exam 4 is on 2/25  Module 4 due 2/29.
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.
Chapter 6 - Visual Basic Schneider
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.
JS Arrays, Functions, Events Week 5 INFM 603. Agenda Arrays Functions Event-Driven Programming.
COMP 1001: Introduction to Computers for Arts and Social Sciences Programming in Scratch Monday, May 16, 2011.
NMED 3850 A Advanced Online Design January 26, 2010 V. Mahadevan.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
WDMD 170 – UW Stevens Point 1 WDMD 170 Internet Languages eLesson: Decision Making with Control Structures and Statements (non-audio version) © Dr. David.
Visual Basic.net Loops. Used to do multiple executions of the same block of code Do while loops Do until loops For next loops.
ㅎㅎ logical operator if if else switch while do while for Third step for Learning C++ Programming Repetition Control Structures.
Coding Design Tools Rachel Gauci. Task: Counting On Create a program that will print out a sequence of numbers from "1" to a "number entered”. Decision’s.
Repetition Structures Repetition Structures allow you to write programs that will repeat program steps multiple times. –Also called Loops –Counter controlled.
1-Dec-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
Pascal Programming Iteration (looping) Carl Smith National Certificate Unit 4.
Loops Robin Burke IT 130. Outline Announcement: Homework #6 Conditionals (review) Iteration while loop while with counter for loops.
ActionScript: For Loops, While Loops, Concatenation and Arrays MMP 220 Multimedia Programming This material was prepared for students in MMP220 Multimedia.
Loops & List Intro2CS – week 3 1. Loops -- Motivation Sometimes we want to repeat a certain set of instructions more than once. The number of repetitions.
1 Printing in Python Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external files.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
CPSC 217 T03 Week V Part #1: Iteration Hubert (Sathaporn) Hu.
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.
COMP Loop Statements Yi Hong May 21, 2015.
XP Tutorial 3 New Perspectives on JavaScript, Comprehensive1 Working with Arrays, Loops, and Conditional Statements Creating a Monthly Calendar.
JavaScript, Sixth Edition
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.
Computer Program Flow Control structures determine the order of instruction execution: 1. sequential, where instructions are executed in order 2. conditional,
Loops and Arrays Chapter 19 and Material Adapted from Fluency Text book.
Computer Programming 12 Lesson 6 – Loop structure By: Dan Lunney.
© 2004 Pearson Addison-Wesley. All rights reserved October 5, 2007 Arrays ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
Week 3.  TO PRINT NUMBERS FROM 1 TO 20  TO PRINT EVEN NUMBERS FROM 1 TO 20 2.
Trace Tables In today’s lesson we will look at:
Lesson #5 Repetition and Loops.
Chapter 9 Repetition.
Tutorial 12 Working with Arrays, Loops, and Conditional Statements
Lesson #5 Repetition and Loops.
Think What will be the output?
Chapter 5: Repetition Structures
While Loops in Python.
Introducing Do While & Do Until Loops & Repetition Statements
Logical Operators and While Loops
Chapter 5 Repetition.
Manipulating Pictures, Arrays, and Loops part 2
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Doing things more than once
Lesson #5 Repetition and Loops.
Chapter 6: Repetition Structures
Chapter 5: Repetition Structures
Chapter (3) - Looping Questions.
Repetition Structures
Week 6 CPS125.
A LESSON IN LOOPING What is a loop?
Loops and Arrays in JavaScript
Lesson #5 Repetition and Loops.
Types of loops definite loop: A loop that executes a known number of times. Examples: Repeat these statements 10 times. Repeat these statements k times.
While Loops in Python.
Flow of Control Flow of control is the order in which a program performs actions. Up to this point, the order has been sequential. A branching statement.
Presentation transcript:

Loops - Monday, Week 4  What are loops?  While loops  Classic loop example - the for loop  Parts of a loop  Examples

What are loops?  Loops are the way we can do something multiple times  Sometimes it’s the same thing repeated, other times we want to change something slightly each repetition.  A loop will repeat a set of statements over and over until a stop condition is met.

While Loops  A while loop repeats a set of statements while a logical expression is true.  For example, we could repeat asking for a big number as long the number given was less than 10.

While Loops  Syntax: while (logical expression) { some statements }  Example: while (myInt < 10) { ask for another integer; }

While Loop Structure Group of Statements A while ( logical expression ) {.. Group of Statements B. } Group of Statements C

Parts of a Loop  Initialize some variables  Loop while some condition is true.  Change some part of the loop conditions so the loop will stop.

Parts of a Loop  In general a for loop makes the parts of a loop explicit.  for (init; test; step) { statements; }  for (i=0; i<11; i++) { document.writeln(“i = “+i+” ”); }

Classic Loop Example  Frequently we want to do something to all items in an array. This requires being able to generate the numbers from 0 to n.  A for loop is an easy way to do this.  Print out the numbers from 0 to 10: for (i=0; i<11; i++) { document.writeln(“i = “+i+” ”); }

Classic Loop Example  Looping through the elements of an array can be done easily with a for loop.  for (var i=0; i < StudentArray.length; i++) { document.writeln (“Name: “ + StudentArray[i].name + “ phone: “ + StudentArray[i].phone + “ ”); }

Examples Placing images on your web page var i; for ( i = 0; i < colors.length; i++ ) { // write document.write('<IMG SRC="'); document.writeln(colors[i]+'ball.gif">'); }

Examples The following for loop and while loops are equivalent: for (i=0; i<11; i++) { document.writeln(“i = “+i+” ”); } i=0; while (i < 11) { document.writeln(“i = “+i+” ”); i++; } What happens in the while loop if you forget the i++?

MiniLab #5  Now we can show our entire array of records using a loop! Then we will put the data into a table format.