Loops Kevin Harville.

Slides:



Advertisements
Similar presentations
Java Control Statements
Advertisements

Looping Structures: Do Loops
CHAPTER 5: LOOP STRUCTURES Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al.
Loops (Part 1) Computer Science Erwin High School Fall 2014.
LOOP / REPETITION while loop. for loop do/while loop We assume that loops are not meant to be infinite. That is, there should always be a way out of the.
Visual Basic.NET BASICS Lesson 10 Do Loops. 2 Objectives Explain what a loop is. Use the Do While and Do Until loops. Use the InputBox function. Use the.
Do Loops A Do..Loop terminates based on a condition that is specified Execution of a Do..Loop continues while a condition is True or until a condition.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Lecture 6: for Loops Yoni Fridman 7/6/01 7/6/01. OutlineOutline  The for statement  The for statement’s format  The for statement’s flow  Comparing.
Repetition Statements repeat block of code until a condition is satisfied also called loops Java supports 3 kinds of loops: while statement – repeats a.
Slides prepared by Rose Williams, Binghamton University Chapter 3 Flow of Control Loops in Java.
1 Loops Loops repeat (iterate) a block of statements for a number of times. A terminating condition tells a loop when to stop iterating (e.g. terminate.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
New Mexico Computer Science For All More Looping in NetLogo Maureen Psaila-Dombrowski.
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.
Lecture 8: Choosing the Correct Loop. do … while Repetition Statement Similar to the while statement Condition for repetition only tested after the body.
CPS120 Introduction to Computer Science Iteration (Looping)
CIS 234: LOOPS Adapted from materials by Dr. Donald Bell, 2000 (updated April 2007)
1 Loops. 2 Topics The while Loop Program Versatility Sentinel Values and Priming Reads Checking User Input Using a while Loop Counter-Controlled (Definite)
Chapter 7 LOOPING OPERATIONS: ITERATION. Chapter 7 The Flow of the while Loop.
1 Conditionals In many cases we want our program to make a decision about whether a piece of code should be executed or not, based on the truth of a condition.
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,
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.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
JavaScript, Fourth Edition
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
CPS120 Introduction to Computer Science Iteration (Looping)
For Code Next For Code Next A loop is a segment of a code that repeats (changing slightly each time)
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
For…Next and Do...While Loops! Happy St. Patrick’s Day!
Controlling Program Flow with Looping Structures
Language Find the latest version of this document at
1 Fall 2009ACS-1903 Ch 4 Loops and Files while loop do-while loop for loop Other topics later on …
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
JavaScript, Sixth Edition
LOOPS CHAPTER Topics  Four Types of Loops –while –do…while –for –foreach  Jump Statements in Loops –break –continue 2.
ECE122 Feb 10, Unary Operator An operator that takes only a single operand Plus: + Minus: – Cast: (type). E.g. (double)
Chapter 6 Controlling Program Flow with Looping Structures.
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.
IST 210: PHP Logic IST 210: Organization of Data IST2101.
PHP Condtions and Loops Prepared by Dr. Maher Abuhamdeh.
FOP: While Loops.
Tutorial 12 Working with Arrays, Loops, and Conditional Statements
Lecture 6 Repetition Richard Gesick.
Loops in Java.
Scratch: iteration / repetition / loops
Miscellaneous Items Loop control, block labels, unless/until, backwards syntax for “if” statements, split, join, substring, length, logical operators,
CiS 260: App Dev I Chapter 4: Control Structures II.
JavaScript: Control Statements.
Lecture 4A Repetition Richard Gesick.
Loop Control Structure.
Loops October 10, 2017.
Chapter 9 Control Structures.
Algorithms Take a look at the worksheet. What do we already know, and what will we have to learn in this term?
M150: Data, Computing and Information
Looping III (do … while statement)
A LESSON IN LOOPING What is a loop?
CHAPTER 21 LOOPS 1.
Fundamental Programming
BIT116: Scripting Lecture 6 Part 1
PROGRAM FLOWCHART Iteration Statements.
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Loops.
LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds.
Review of Previous Lesson
LOOPING STRUCTURE Chapter - 7 Padasalai
Chapter 8 JavaScript: Control Statements, Part 2
Presentation transcript:

Loops Kevin Harville

for loops Loop, through the braced code starting with myVar at 1. for (myVar = 1; myVar<=10; myVar + 2){ alert(myVar) } Loop, through the braced code starting with myVar at 1. Continue as long as myVar is <= 10 Increment by 2

while loops while (condition == true){ alert("True"); //maybe the condition changes } While loops repeat as long as the condition remains true.

while loops //previous statements … while (condition == true){ alert("True"); } //subsequent statements… The loop would be passed over if the condition were never true--Just like an if statement.

do while loops do { alert("True") //maybe the condition changes } while (condition == true); When the program reaches this point, the loop would execute at least once. The condition is at the end of the loop.

Breaking out while (condition == true){ alert("True"); if (tired == true) break; } //subsequent lines While loops repeat as long as the condition remains true….unless we break out of the loop. For loops repeat their iterations* as scheduled until a break is reached. *Iteration: Geekspeak for times.

continue var x = 5; while (x < 10) { x++; if (x == 7) continue; document.write(x + "<BR>"); } Continue continues on with the next iteration of the loop.

for … in loops Similar to the previous but cycle through all properties of an object. Know they exist. They are handy, particularly for debugging. for (i in document) { document.write("<p>" + i ="<br>"); document.write(document[i]); }

Loops and Arrays Loops are very often used with arrays. Remember, arrays are objects that consist of several subscripted variables: myArray = new Array(); myArray[0] = 24 myArray[1]= 12 myArray[2] = 4

Looping through an Array for (i=0; i <= 2; i++) { alert(myArray[i]) }

Putting it all together: myArray = new Array(); myArray[0] = 24 myArray[1]= 12 myArray[2] = 4 for (i=0; i <= 2; i++) { alert(myArray[i]) }

Summary of Loops for loop while loop do / while loop for / in loop for use with object properties break finishes the loop altogether continue goes directly to the next iteration Much can be accomplished with arrays and loops