JavaScript 101 Lesson 8: Loops.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Repetition Statements Perform the same task repeatedly Allow the computer to do the tedious, boring things.
Programming In C++ Spring Semester 2013 Lecture 3 Programming In C++, Lecture 3 By Umer Rana.
Java Script Session1 INTRODUCTION.
CHAPTER 5: Repetition Control Structure. Objectives  To develop algorithms that use DOWHILE and REPEAT.. UNTIL structures  Introduce a pseudocode for.
CS0004: Introduction to Programming Repetition – Do Loops.
Lesson 4: Formatting Input Data for Arithmetic
CIS101 Introduction to Computing Week 12. Agenda Your questions Solutions to practice text Final HTML/JavaScript Project Copy and paste assignment JavaScript:
JavaScript 101 Lesson 01: Writing Your First JavaScript.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 5: Looping by Tony.
CIS101 Introduction to Computing Week 11. Agenda Your questions Copy and Paste Assignment Practice Test JavaScript: Functions and Selection Lesson 06,
JavaScript 101 Lesson 5: Introduction to Events. Lesson Topics Event driven programming Events and event handlers The onClick event handler for hyperlinks.
1 Shortcuts for Lazy Programmers! Topics Increment and Decrement Operators Assignment Operators.
CIS101 Introduction to Computing Week 12 Spring 2004.
New Mexico Computer Science For All More Looping in NetLogo Maureen Psaila-Dombrowski.
Chapter 4 Loops Write code that prints out the numbers Very often, we want to repeat a (group of) statement(s). In C++, we have 3 major ways of.
CHAPTER 3 CONTROL STRUCTURES ( REPETITION ) I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
JavaScript, Fourth Edition
Chapter 5: Control Structures: Iteration Visual Basic.NET Programming: From Problem Analysis to Program Design.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
JavaScript 101 Lesson 3: Variables and Arithmetic.
Repetition Control Structure. Introduction Many applications require certain operations to be carried out more than once. Such situations require repetition.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
CISW CRC - Fishman 2014 / 2015 PRESENTATION 4b: CISW 400 (Online) – Client-side Scripting for the Internet Cosumnes River College Fishman – 2015.
Beginning C For Engineers Fall 2005 Lecture 3: While loops, For loops, Nested loops, and Multiple Selection Section 2 – 9/14/05 Section 4 – 9/15/05 Bettina.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
JavaScript 101 Lesson 6: Introduction to Functions.
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.
Topic : While, For, Do-While Loop Guided By : Branch : Batch :
Using DHTML to Enhance Web Pages
Using JavaScript to Show an Alert
Chapter 6: Loops.
CHAPTER 6: REPETITION AND LOOP STATEMENTS
Lecture 6 Repetition Richard Gesick.
JavaScript Loops.
Chapter 5: Loops and Files.
Chapter 5: Repetition Structures
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
Chapter 5: Looping Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Beginning C Lecture 4 Lecturer: Dr. Zhao Qinpei
Manipulating Pictures, Arrays, and Loops part 2
Lecture 4A Repetition Richard Gesick.
Arrays, For loop While loop Do while loop
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
Lesson 05: Iterations Topic: Introduction to Programming, Zybook Ch 4, P4E Ch 5. Slides on website.
Alternate Version of STARTING OUT WITH C++ 4th Edition
Lecture Notes – Week 3 Lecture-2
Chapter 6: Repetition Structures
Chapter 5: Repetition Structures
Lesson 2: Input and Variables
Chapter (3) - Looping Questions.
Loops CIS 40 – Introduction to Programming in Python
Faculty of Computer Science & Information System
Chapter 6: Repetition Statements
3.1 Iteration Loops For … To … Next 18/01/2019.
For this assignment, copy and past the XHTML to a notepad file with the .html extension. Then add the code I ask for to complete the problems.
More Loops Topics Counter-Controlled (Definite) Repetition
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
More Loops Topics Counter-Controlled (Definite) Repetition
Assignment Operators Topics Increment and Decrement Operators
CSC1401 Manipulating Pictures 2
Introduction to JavaScript
Web Programming and Design
Web Programming and Design
More Loops Topics Counter-Controlled (Definite) Repetition
Presentation transcript:

JavaScript 101 Lesson 8: Loops

Lesson Topics Introduction to loops The advantages of loops Increment and decrement operators For loop and while loop syntax Controlling loops with conditional expressions

Loops A powerful feature computers is the ability to repeat steps over and over as many times as necessary Programming languages include statements that define a section of code that repeats The generic name for a programming statement that repeats is a loop The set of statement which is repeatedly executed is called the body of the loop

Controlling loops Loops repeat code over and over, but eventually they do have to stop Common way to control loops is to repeat a section of code a certain number of times This requires counting how many times the loop has repeated, then stopping when the right number has been reached

Counting with the increment and decrement operators JavaScript uses the increment and decrement operators for counting Increment means increase (get larger), decrement means decrease (get smaller)

Increment operator The increment operator ++ adds 1 to the value of a variable Example: num++; ++num; both add 1 to value in num Can use ++ before or after the variable

Decrement Operator The increment operator -- subtracts 1 from the value of a variable Example: num--; --num; both subtract 1 from value in num Can use -- before or after the variable

The for Loop The for loop repeats a section of code a specific number of times Syntax: for (expression1;expression2;expression3) { statements to be repeated go here }

The for Statement (cont.) The for statement has three parts part 1: Initializes the control variable in expression1 part 2: Tests expression2. If it is true, the body of the loop repeats part 3: Update the value of the control variable using expression3 Repeats steps 2 and 3,until expression2 becomes false

The while Loop The while loop is repeats until its condition is false Syntax: while (condition) { Statements to be repeated } Where condition is a true or false test

The while Loop (cont.) The while loop is implemented using the following steps Step 1: The condition is tested to see whether it is true or false Step 2: If condition is true, the loop body repeats Repeats steps 1 and 2 until condition becomes false

In the lab You will use loop statements in your code Open Notepad and create a new HTML document named lesson0801.html Enter the code on p. 8-10 exactly as you see it Save the file and open it using either Internet Explorer or Netscape

Student Modifications Execute the code on p. 8-10 using different values Find and display another image in the loop body Re-write the code using a while loop instead of a for loop

Student Modifications (cont.) Modify the code on p. 8-12 as follows: Add a second button that calls the function shake() with a different value for the parameter Use the onLoad event handler to shake your window as soon as it loaded

Lesson Summary Loops are sections of code that repeat over and over JavaScript statements that repeat include the for loop and the while loop The increment operator and the decrement operator are used to control loops How to use the onLoad event handler