Executes a statement or statements for a number of times – iteration. Syntax for(initialize; test; increment) { // statements to be executed } Initial.

Slides:



Advertisements
Similar presentations
CS 101 Introductory Programming - Lecture 7: Loops In C & Good Coding Practices Presenter: Ankur Chattopadhyay.
Advertisements

Looping Structures: Do Loops
How SAS implements structured programming constructs
LECTURE 9 21/11/12 1. Comments in JavaScript // This is a single line JavaScript comment 2.
14/11/11.  These words have special meanings in themselves  These should NOT be used as Identifiers.  For example:  Break, do, function, case, else,
Tutorial 12 Working with Arrays, Loops, and Conditional Statements
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Repetition Statements repeat block of code until a condition is satisfied also called loops Java supports 3 kinds of loops: while statement – repeats a.
Some loop programs 1: Read in 10 integers and output their sum
CS1061: C Programming Lecture 8: Repetition A. O’Riordan, 2004.
Week 11 Recap CSE 115 Spring Want to write a programming language? You’ll need three things: You’ll need three things: –Sequencing –Selection Polymorphism.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Copyright © Texas Education Agency, Computer Programming For Loops.
JavaScript Switch Statement. Switch JavaScript Switch Statement If you have a lot of conditions, you can use a switch statement instead of an if…elseif…
Loops Doing the same thing over and over and over and over and over and over…
Engineering 1020 Introduction to Programming Peter King Winter 2010.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Chapter 6 - VB 2005 by Schneider1 Chapter 6 – Repetition 6.1 Do While and Do Until Loops 6.2 Processing Lists of Data with Do Loops 6.3 For...Next Loops.
CPS120 Introduction to Computer Science Iteration (Looping)
Computer Science Department LOOPS. Computer Science Department Loops Loops Cause a section of your program to be repeated a certain number of times. The.
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,
LOGO Conditional Statements & Loops in JavaScript CHAPTER 10 Eastern Mediterranean University School of Computing and Technology Department of Information.
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.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 ‏ Control Structures.
1 CSCI N201 Programming Concepts and Database 9 – Loops Lingma Acheson Department of Computer and Information Science, IUPUI.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
JavaScript, Fourth Edition
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 10 - JavaScript/JScript: Control Structures II Outline 10.1Introduction 10.2Essentials of.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
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)
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
1 Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
REPETITION STATEMENTS - Part1  Also called LOOP STATEMENTS OR LOOP STRUCTURES 1 C++ Statements that repeat one or more actions while some condition is.
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.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Chapter 7: Repetition Structure (Loop) Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills.
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.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 9 - JavaScript: Control Structures II Outline 9.1 Introduction 9.2 Essentials of Counter-Controlled.
Loops Tonga Institute of Higher Education. Introduction Programs need to be able to execute tasks repeatedly. Use loops to repeat actions  For Loop 
JavaScript JavaScript ( Condition and Loops ). Conditional Statements If Statement If...else Statement if (condition) { code to be executed if condition.
Learning Javascript From Mr Saem
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
26/06/ Iteration Loops For … To … Next. 226/06/2016 Learning Objectives Define a program loop. State when a loop will end. State when the For.
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.
LESSON 5 Loop Control Structure. Loop Control Structure  Operation made over and over again.  Iterate statement.
PHP Condtions and Loops Prepared by Dr. Maher Abuhamdeh.
Branching Constructs Review
CiS 260: App Dev I Chapter 4: Control Structures II.
Ch 7: JavaScript Control Statements I.
JavaScript: Control Statements.
LESSON 11 – WHILE LOOPS UNIT 5 – 1/10/17.
Loop Control Structure.
LRobot Game.
T. Jumana Abu Shmais – AOU - Riyadh
JavaScript: Control Statements II
Seating “chart” Front - Screen rows Back DOOR.
Repetition Statements (Loops) - 2
Based on slides created by Bjarne Stroustrup & Tony Gaddis
PROGRAM FLOWCHART Iteration Statements.
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Presentation transcript:

Executes a statement or statements for a number of times – iteration. Syntax for(initialize; test; increment) { // statements to be executed } Initial Value is the starting number of the loop. If the condition(test) is true the code is executed. The initial value is changed by the step size. 1 For Loops

The expression is evaluated, if it is false, JavaScript moves to the next statement in the program If it is true then the statement is executed and expression is evaluated again Again if the expression is false the JavaScript moves on the next statement in script, otherwise it executes the statement that forms the body of the loop 2 For Loops

For for (i=0;i<= 5; i++) //the position of the ++ here does not have an impact on the output it will always start at 0 { document.write("The number is " + i);// change to i++ and ++i here to see the impact on the output document.write(" "); } 3 For Loops

Another Example For for (i=0;i<= 5; i++) //the position of the ++ here does not have an impact on the output it will always start at 00 { document.write("The number is " + i++ ); document.write(" "); } The number is 0 The number is 2 The number is 4 4

Another Example For for (i=0;i<= 5; i++) //the position of the ++ here does not have an impact on the output it will always start at 00 { document.write("The number is " + ++i);// document.write(" "); } The number is 1 The number is 3 The number is 5 5

While a condition is true execute one or more statements. “While Loops” are especially useful when you do not know how many times you have to loop but you know you should stop when you meet the condition, i.e. an indeterminate loop 6 While Loops:

The while Loop is the basic statement that allows JavaScript to perform repetitive actions. It has the following syntax. while (expression) { // Statement to be executed } … more code… 7 While Loop

This cycle continues until the expression evaluates to false It is important that a Loop counter that is used within the body of the Loop is declared outside it. 8 While Loops

i=0; while (i<=5) { document.write("The number is " + i); document.write(" "); i++; } 9

While Loops i=1; while (i<=5) { document.write("The number is " + i); document.write(" "); i++; } What happens in this example? i=6; while (i<=5) { document.write("The number is " + i); document.write(" "); i++; } Loop DOES not run 10

Do While Loops var i=0; do { document.write("Output= " + i); document.write(" "); i++; } while (i<=12); Will run once to evaluate condition – var i=13; do { document.write("Output= " + i); document.write(" "); i++; } while (i<=12); 11

Why use Loops? When you want the same block of code to run over and over again in a row Instead of adding several almost equal lines in a script we can use loops to perform a task like this While v For A while loop doesn't initialize or increment any fields automatically as part of the command, it just tests a condition and executes the loop for as long as the condition remains true A while loop can easily be substituted wherever you have a for loop by moving the initialization statement (e.g. i=1) in front of the loop and the increment statement (e.g. i++) to just inside the end of the loop This may make the loop easier to read 12