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.

Slides:



Advertisements
Similar presentations
Loops (Part 1) Computer Science Erwin High School Fall 2014.
Advertisements

1 CS101 Introduction to Computing Lecture 23 Flow Control & Loops (Web Development Lecture 8)
PHP Functions and Control Structures. 2 Defining Functions Functions are groups of statements that you can execute as a single unit Function definitions.
Executes a statement or statements for a number of times – iteration. Syntax for(initialize; test; increment) { // statements to be executed } Initial.
Branching Constructs Review l what are branching constructs? what type of branching constructs have we studied? l what is nested if? l what is multiway.
COMP 14 Introduction to Programming Miguel A. Otaduy May 21, 2004.
Tutorial 12 Working with Arrays, Loops, and Conditional Statements
AdvWeb-1/14 DePaul University SNL 262 Advanced Web Page Design Chapt 8 - Looping Instructor: David A. Lash.
Iteration This week we will learn how to use iteration in C++ Iteration is the repetition of a statement or block of statements in a program. C++ has three.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Chapter 5: Control Structures II (Repetition)
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.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements II.
CS 106 Introduction to Computer Science I 09 / 28 / 2007 Instructor: Michael Eckmann.
Introduction to JavaScript for Python Programmers
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…
TODAY’S LECTURE Review Chapter 2 Go over exercises.
Control Structures II. Why is Repetition Needed? There are many situations in which the same statements need to be executed several times. Example: Formulas.
Chapter 4: Control Structures II
Conditional Statements While writing a program, there may be a situation when you need to adopt one path out of the given two paths. So you need to make.
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.
Principles of Programming - NI July Chapter 5: Structured Programming In this chapter you will learn about: Sequential structure Selection structure.
CSEB 114: PRINCIPLE OF PROGRAMMING Chapter 5: Structured Programming.
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)
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
LOOPS In the Name of Allah The Most Merciful The Most Compassionate LOOPS
Control Structures II (Repetition). Objectives In this chapter you will: Learn about repetition (looping) control structures Explore how to construct.
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,
LOGO Conditional Statements & Loops in JavaScript CHAPTER 10 Eastern Mediterranean University School of Computing and Technology Department of Information.
Chapter 5: Structured Programming
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 ‏ Control Structures.
Loops George Mason University. Loop Structure Loop- A structure that allows repeated execution of a block of statements Loop body- A block of statements;
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.
CIS 133 Mashup Javascript, jQuery and XML Chapter 3 Building Arrays and Controlling Flow.
CPS120 Introduction to Computer Science Iteration (Looping)
Loops cause a section of a program to be repeated a certain number of times. The repetition continues while a condition remains true. When a condition.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure /
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.
COMP Loop Statements Yi Hong May 21, 2015.
1 CS428 Web Engineering Lecture 13 Flow Control & Loops (JavaScript - III)
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
JavaScript and Ajax (Control Structures) Week 4 Web site:
JavaScript, Sixth Edition
 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 IN ‘C’ PROGRAMMING. V ERY O FTEN, Y OU W ILL W ANT TO D O S OMETHING M ORE T HAN O NCE HA.
Learning Javascript From Mr Saem
JavaScript Tutorial First lecture 19/2/2016. Javascript is a dynamic computer programming language. It is lightweight and most commonly used as a part.
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.
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
Tutorial 12 Working with Arrays, Loops, and Conditional Statements
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.
CiS 260: App Dev I Chapter 4: Control Structures II.
JavaScript: Control Statements.
JavaScript: Control Statements I
Arrays, For loop While loop Do while loop
Loop Control Structure.
MSIS 655 Advanced Business Applications Programming
Conditional Statements & Loops in JavaScript
BIT116: Scripting Lecture 6 Part 1
PROGRAM FLOWCHART Iteration Statements.
Chapter 8 JavaScript: Control Statements, Part 2
Presentation transcript:

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 to write loop statements to reduce the number of lines. JavaScript supports all the necessary loops to help you on all steps of programming.

Kinds Of Loops The for loop is used when you know in advance how many times the script should perform. For example if you wanted it to create exactly 50 lines. The while loop is used when you want the loop to continue until a certain condition becomes true. For example, if you wanted to make a table comparing Celsius and Fahrenheit, stepping 15 degrees for each row, and you wanted the table to contain values up to 1200 degrees of Celsius.

FOR LOOPS SYNTAX: for (variable= startvalue; variable<=endvalue; variable = variable + incrementfactor) { // Here goes the script lines you want to loop. } The for loop is the most compact form of looping.

For Loop For Loop includes the following three important parts: 1.The loop initialization where we initialize our counter to a starting value. The initialization statement is executed before the loop begins. 2.The test statement which will test if the given condition is true or not. If condition is true then code given inside the loop will be executed otherwise loop will come out.

For Loop 3.The iteration statement where you can increase or decrease your counter. You can put all the three parts in a single line separated by a semicolon.

Code example var count; document.write("Starting Loop" +" "); for(count = 0; count < 10; count++) { document.write("Current Count : " + count ); document.write(" "); } document.write("Loop stopped!");

The while Loop The most basic loop in JavaScript is the while loop. Syntax: while (expression) { Statement(s) to be executed if expression is true } The purpose of a while loop is to execute a statement or code block repeatedly as long as expression is true. Once expression becomes false, the loop will be exited.

Example: Following example illustrates a basic while loop: var count = 0; document.write("Starting Loop" + " "); while (count < 10) { document.write("Current Count : " + count + " "); count++; } document.write("Loop stopped!");

The do...while Loop The do...while loop is similar to the while loop except that the condition check happens at the end of the loop. This means that the loop will always be executed at least once, even if the condition is false.

Syntax: do-while loop Do { Statement(s) to be executed; } while (expression); Note the semicolon used at the end of the do...while loop.

Example: Let us write above example in terms of do...while loop. var count = 0; document.write("Starting Loop" + " "); do{ document.write("Current Count : " + count + " "); count++; } while (count < 0); document.write("Loop stopped!");

For...In Loop There is one more loop supported by JavaScript. It is called for...in loop. This loop is used to loop through an object's properties. Because we have not discussed Objects yet, so you may not feel comfortable with this loop. But once you will have understanding on JavaScript objects then you will find this loop very useful.

Syntax: For...In Loop for (variablename in object) { statement or block to execute } In each iteration one property from object is assigned to variablename and this loop continues till all the properties of the object are exhausted.

Example: Here is the following example that prints out the properties of a Web browser's Navigator object: var aProperty; document.write(" Navigator Object Properties "); for (aProperty in navigator) { document.write(aProperty); document.write(" "); } document.write("Exiting from the loop!");

The break Statement The break statement, which was briefly introduced with the switch statement, is used to exit a loop early, breaking out of the enclosing curly braces.

Example: var x = 1; document.write("Entering the loop "); while (x < 20) { if (x == 5) { break; // breaks out of loop completely } x = x + 1; document.write( x + " "); } document.write("Exiting the loop! ");

The continue Statement The continue statement tells the interpreter to immediately start the next iteration of the loop and skip remaining code block. When a continue statement is encountered, program flow will move to the loop check expression immediately and if condition remain true then it start next iteration otherwise control comes out of the loop.