Looping Constructs “Here we go loop de loop, on a Saturday night” – Johnny Thunder “First I'm up, and then I'm down. Then my heart goes around and around.”

Slides:



Advertisements
Similar presentations
Chapter 13 Control Structures. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Control Structures Conditional.
Advertisements

CMPS 1371 Introduction to Computing for Engineers
Creating PHP Pages Chapter 7 PHP Decisions Making.
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.
Loops (Part 1) Computer Science Erwin High School Fall 2014.
ITC 240: Web Application Programming
Computer Science 1620 Loops.
Introduction to Computers and Programming Lecture 9: For Loops New York University.
Iteration (Looping Constructs in VB) Iteration: Groups of statements which are repeatedly executed until a certain test is satisfied Carrying out Iteration.
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, © Copyright 2010 All Rights Reserved. 1.
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.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, © Copyright 2010, All Rights Reserved 1.
SM1205 Interactivity Topic 06: Iteration and Multiple Objects Spring 2010SCM-CityU1.
Introduction to JavaScript for Python Programmers
JS Arrays, Functions, Events Week 5 INFM 603. Agenda Arrays Functions Event-Driven Programming.
Arrays and Control Structures CST 200 – JavaScript 2 –
Ternary Operators © Copyright 2014, Fred McClurg All Rights Reserved.
Chapter 5: Control Structures II (Repetition)
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
Do … while ( continue_cond ) Syntax: do { stuff you want to happen in the loop } while (continue_condition);
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
Lecture 4 Introduction to Programming. if ( grade ==‘A’ ) cout
Lecture 8: Choosing the Correct Loop. do … while Repetition Statement Similar to the while statement Condition for repetition only tested after the body.
Loops and Iteration for Statements, while Statements and do-while Statements.
MATLAB for Engineers 4E, by Holly Moore. © 2014 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is protected by Copyright.
PHP Programming with MySQL Slide 4-1 CHAPTER 4 Functions and Control Structures.
Chapter 2 Functions and Control Structures PHP Programming with MySQL 2 nd Edition.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
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,
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.
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.
PHP Constructs Advance Database Management Systems Lab no.3.
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.
Repetition Control Structures Simple Program Design Third Edition A Step-by-Step Approach 5.
Math 15 Introduction to Scientific Data Analysis Lecture 8 Python Programming – Part 2 University of California, Merced.
Iteration Chapter Iteration = Repetition 3 Looping Via The for Loop for loop: A block of code that executes a group of statements repeatedly until.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, © Copyright 2015, Fred McClurg, All Rights.
JavaScript, Fourth Edition
Pascal Programming Iteration (looping) Carl Smith National Certificate Unit 4.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Conditional Statements © Copyright 2014, Fred McClurg All Rights Reserved.
Conditional Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Loops Robin Burke IT 130. Outline Announcement: Homework #6 Conditionals (review) Iteration while loop while with counter for loops.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
Repetition Control Structure. Introduction Many applications require certain operations to be carried out more than once. Such situations require repetition.
Iteration. Iteration: Review  If you wanted to display all the numbers from 1 to 1000, you wouldn’t want to do this, would you? Start display 1 display.
Custom Functions © Copyright 2014, Fred McClurg All Rights Reserved.
1 CS428 Web Engineering Lecture 13 Flow Control & Loops (JavaScript - III)
ALGORITHMS AND FLOWCHARTS. Why Algorithm is needed? 2 Computer Program ? Set of instructions to perform some specific task Is Program itself a Software.
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
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.
Repetition Looping. Types for while do..while for The for loop allows you to iterate through a variable for a specific range of values. You must supply.
Learning Javascript From Mr Saem
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, © Copyright 2016, Fred McClurg, All Rights.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
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 Structures Chapter 9
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.
Arrays, For loop While loop Do while loop
JavaScript Selection Statement Creating Array
Introduction to Programming
Java Programming Loops
A LESSON IN LOOPING What is a loop?
PROGRAM FLOWCHART Iteration Statements.
Python While Loops.
Presentation transcript:

Looping Constructs “Here we go loop de loop, on a Saturday night” – Johnny Thunder “First I'm up, and then I'm down. Then my heart goes around and around.” – Yo-Yo, The Osmonds © Copyright 2014, Fred McClurg All Rights Reserved

“while” Loop Flow Chart Description:  As long as the condition is true, the “while” loop executes the block of code.  When the condition is false, the loop exits.  Notice that the condition is at the top of the loop. 2 WHILE Code Block true false

What is a “while” loop? Description: The “while” loop executes a block of code zero or more times. Syntax: while ( condition ) { // true // block of code; } Best used when... This looping construct is used when you don’t know the number of iterations. Also ideal for when the end condition is not well defined or you may wish to protect the block from execution. 3

Simple “while” Loop Discussion: The condition is at the top of the loop and the increment is inside the body of the loop. Example: var i = 0; // initialize index while ( i < 10 ) { // if true console.log( i ); i++; // increment } 4 while.html

Complex “while” Loop var ingalls = [ "Charles", "Caroline", "Mary", "Laura", "Carrie" ]; while ( ingalls.length != 0 ) { console.log( "Array: " + ingalls + " Length: " + ingalls.length ); person = ingalls.pop(); console.log( "Popped: " + person ); } 5 whilePop.html

“do while” Loop Flow Chart Description:  As long as the condition is true, the “do while” loop executes the block of code.  When the condition is false, the loop exits.  Notice that the condition is at the bottom of the loop. 6 DO WHILE DO WHILE Code Block true false

What is a “do while” loop? Description: The “do while” loop executes a block of code one or more times. Syntax: do { // while // block of code; } while ( condition ); // notice semi-colon Best used when... Looping construct is used in those situations when you don’t know the number of iterations. Ideal for when the end condition is not defined and you need to execute the block at least one time, perhaps more. Also called a “Repeat Until” false loop. 7

“do while” Student Exercise Description: Create a script that uses a “do while” loop to print the numbers 0 to 9 to the console. Hint: To get started, look at the “do while” syntax. Also look at the first “while” example code. The solution for this exercise will be similar to the example. 8

Simple “do while” Loop Discussion: The condition is at the bottom of the loop and the increment is inside the body of the loop. Example: var i = 0; // initialize counter do { // while console.log( i ); i++; // increment } while ( i < 10 ); // semi-colon 9 doWhile.html

Complex “do while” and “random()” // count number of times to draw a queen (using a do while) var guess = 0; // number of guesses var cards = [ "Ace", "King", "Queen", "Jack" ]; // face cards var min = 0; // first array index var max = cards.length - 1; // largest index do { // while () guess++; // pick random number between 0 and 3 var pick = Math.floor( (Math.random() * max) + min ); if ( cards[pick] != "Queen" ) // wrong guess console.log( guess + ". Wrong: " + cards[pick] ); } while( cards[pick] != "Queen" ); // remember semi-colon! console.log( guess + ". Right: " + cards[pick] ); console.log( "Guesses: " + guess ); // total tries 10 doWhile.html

What is a “for” loop? Description: A looping construct that is ideal when the exact number (or the max number) of iterations is known. Syntax: for ( initialization; // semi-colon condition; // semi-colon increment|decrement ) { // block of code; } 11

Simple “for” Loop Description: Display a count from 0 to 9. Example: var min = 0; var max = 10; for ( var i = min; i < max; i++ ) { console.log( i ); } 12 for.html

“for” Student Exercise Description: Create a script that uses a “for” loop to count by fives from 0 to 25 and print the results to the console. Hint: To get started, look at the Simple “for” Loop example (previous slide). There are two ways to solve this exercise: 1.Use a modulo inside an “if” statement. 2.Or use a short cut operator. 13

Multiples via “for”, “if”, and “%” Discussion: Use the modulo operator within an “if” statement to determine if a number is an even multiple. Example: var min = 0; var max = 25; for ( var i = min; i <= max; i++ ) { // if ( i % 5 ) { // would this work also? if ( ( i % 5 ) == 0 ) { // even multiple console.log( i ); } 14 forIfModulo.html

Multiples via “for” and “+=” Discussion: Use the “ += “ short-cut operator to count by fives. This solution would execute faster because the loop has to perform fewer iterations. In addition, it is simpler because it contains less lines of code. Example: var min = 0; var max = 25; for ( var i = min; i <= max; i += 5 ) { console.log( i ); } 15 forPlusEqual.html

Array Iteration with “for” Description: Iterate through an array and determine if there is a match to the current document title. Example: var pageTitles = [ 'Home', 'About', 'Contact', 'Array Iteration with \"for\"' ]; for ( var i = 0; i < pageTitles.length; i++ ) { // title of current page in document.title if ( document.title == pageTitles[i] ) console.log( "Current page: " + pageTitles[i] ); else console.log( "Not current page: " + pageTitles[i] ); } 16 whereAmI.html

What is a “for in” loop? Description: A looping construct designed to iterate over the elements of an array. Syntax: for ( var index in arrayName ) { statement;...; } 17

A “for in” Loop Example Description: Looping construct designed to iterate over the elements of an array. Example: var ohMy = [ "Lions", "Tigers", "Bears" ]; for ( var i in ohMy ) { console.log( "ohMy[" + i + "] = " + ohMy[i] ); } 18 forIn.html