CSCI N201: Programming Concepts Copyright ©2005  Department of Computer & Information Science Introducing JavaScript Loops.

Slides:



Advertisements
Similar presentations
1 Chapter Five Selection and Repetition. 2 Objectives How to make decisions using the if statement How to make decisions using the if-else statement How.
Advertisements

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.
Repeating Actions While and For Loops
Do/Loops A loop repeats a series of instructions. An iteration is a single execution of the statement(s) in the loop. Used when the exact number of iterations.
Executes a statement or statements for a number of times – iteration. Syntax for(initialize; test; increment) { // statements to be executed } Initial.
Iteration (Looping Constructs in VB) Iteration: Groups of statements which are repeatedly executed until a certain test is satisfied Carrying out Iteration.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
An Introduction to Programming with C++ Fifth Edition
CS1061: C Programming Lecture 8: Repetition A. O’Riordan, 2004.
Computer Programming 1 Repetition. Computer Programming 2 Objectives Repetition structures Study while and do loops Examine for loops A practical example.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand basic loop concepts: ■ pretest loops and post-test loops ■ loop.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
CSCI N201: Programming Concepts Copyright ©2005  Department of Computer & Information Science Working with Loops.
11 Chapter 4 LOOPS AND FILES. 22 THE INCREMENT AND DECREMENT OPERATORS To increment a variable means to increase its value by one. To decrement a variable.
Repetition Statements Repeating an Action A specified number of times While a Condition is True Until a Condition is True.
CSCI N201: Programming Concepts Copyright ©2005  Department of Computer & Information Science Using Decision Structures.
Do … while ( continue_cond ) Syntax: do { stuff you want to happen in the loop } while (continue_condition);
Lecture 10: Reviews. Control Structures All C programs written in term of 3 control structures Sequence structures Programs executed sequentially by default.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Programming with C# Iteration LECTURE 3. Summary of last lecture SequenceSelectionif and switch statementsCastingRandom.
CPS120 Introduction to Computer Science Iteration (Looping)
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
ㅎㅎ logical operator if if else switch while do while for Third step for Learning C++ Programming Repetition Control Structures.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 5: Introduction to C: More Control Flow.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 ‏ Control Structures.
Overview of Java Loops By: Reid Hunter. What Is A Loop? A loop is a series of commands that will continue to repeat over and over again until a condition.
Repetition. Control of Flow SEQUENCE SELECTION (if..else, switch…case) REPETITION.
Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 9 & 10 Repetition Statements.
Introduction to Problem Solving and Control Statements.
Statement Level Flow of Control Iteration Structures Copyright © by Curt Hill.
ITERATIVE STATEMENTS. Definition Iterative statements (loops) allow a set of instruction to be executed or performed several until condition are met.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
1 A Balanced Introduction to Computer Science, 2/E David Reed, Creighton University ©2008 Pearson Prentice Hall ISBN Chapter 13 Conditional.
Using Loops. Goals Understand how to create while loops in JavaScript. Understand how to create do/while loops in JavaScript. Understand how to create.
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.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
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 7: The Repetition Structure Introduction to Programming with C++ Fourth Edition.
Chapter 7: Repetition Structure (Loop) Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
CC213 Programming Applications Week #2 2 Control Structures Control structures –control the flow of execution in a program or function. Three basic control.
COMPUTER PROGRAMMING I 5.05 Apply 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.
Follow up from lab See Magic8Ball.java Issues that you ran into.
Repetition Structures Chapter 9
Ch 7: JavaScript Control Statements I.
Web Programming– UFCFB Lecture 16
Control Structure Senior Lecturer
Chapter 6 Repetition Objectives ❏ To understand basic loop concepts:
Looping and Repetition
Understanding the Three Basic Structures
Chapter 6: Repetition Statements
Computer Science Core Concepts
ICT Programming Lesson 3:
Using Decision Structures
Seating “chart” Front - Screen rows Back DOOR.
FLUENCY WITH INFORMATION TECNOLOGY
PROGRAM FLOWCHART Iteration Statements.
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.
Presentation transcript:

CSCI N201: Programming Concepts Copyright ©2005  Department of Computer & Information Science Introducing JavaScript Loops

CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science Goals Understand how to create while loops in JavaScript.Understand how to create while loops in JavaScript. Understand how to create do/while loops in JavaScript.Understand how to create do/while loops in JavaScript. Understand how to create simple for (counting) loops in JavaScript.Understand how to create simple for (counting) loops in JavaScript.

CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science Computing Structures Remember, last time we identified that all executable statements fall into one of three categories:Remember, last time we identified that all executable statements fall into one of three categories: –Sequential Structures – those structures where instructions happen in sequence. That is “A before B”, “B before C”, “C before D”, etc. –Looping Structures – Today’s unit. –Decision (Selection) Structures – those structures where code alternate executes based on some Boolean test.

CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science What is a Loop? A loop is a programming structure that contains code that will repeat until that code causes something to happen to satisfy or to not satisfy a given condition, thus ending the loop.A loop is a programming structure that contains code that will repeat until that code causes something to happen to satisfy or to not satisfy a given condition, thus ending the loop. There are two basic “families” of loops:There are two basic “families” of loops: –Conditional Loops: Loops that depend solely on some type of Boolean test. –Iterative Loops (a.k.a. For, For … Next loops): Loops that depend on matching a maximum or minimum number of iterations (repetitions).

CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science Parts of a Loop All loops share some basic parts. These include:All loops share some basic parts. These include: –Condition to test: This can be a Boolean test (for conditional loops) or a test against a maximum or minimum integer value (for iterative loops). –Executable block: The block of code that will execute so long as the loop has/hasn’t yet satisfied the condition tested. –A way to end the loop: In terms of syntax, this is not necessary, but forgetting to include the method for ending the loop somewhere (usually the executable block) results in an endless loop.

CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science Conditional Loops Conditional loops are based on some type of Boolean test.Conditional loops are based on some type of Boolean test. Conditional loops are useful when the loop’s executable block should execute for an indeterminate length of time. You, as the programmer, don’t know how many times the executable block will execute in practice.Conditional loops are useful when the loop’s executable block should execute for an indeterminate length of time. You, as the programmer, don’t know how many times the executable block will execute in practice.

CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science Conditional Loops Conditional loops are often defined by where the condition is written, in reference to the executable block:Conditional loops are often defined by where the condition is written, in reference to the executable block: –Pre-test loops: The condition is located before the executable block. There is a possibility that a pre-test loop may never execute. In JavaScript, the while loop is a type of pre-test loop. –Post-test loops: The condition is located after the executable block. A pre-test loop’s executable block will always execute at least once. In JavaScript, the do … while loop is a type of post- test loop.

CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science The “while” Loop Allows repeatable code until a given condition is metAllows repeatable code until a given condition is met Useful when the program should go on for an indeterminate length of timeUseful when the program should go on for an indeterminate length of time Must have a way of terminating the structure from within the loop!Must have a way of terminating the structure from within the loop! Pre-Test Loop (Loop may NEVER execute)Pre-Test Loop (Loop may NEVER execute)

CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science 3 Basic Parts of a While Loop The condition being testedThe condition being tested The action to be performed while the condition is evaluated to be trueThe action to be performed while the condition is evaluated to be true A method by which the condition can be evaluated to be false, ending the while loopA method by which the condition can be evaluated to be false, ending the while loop

CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science While Loop – Everyday Example while (sign is not a stop sign) { keep driving; recognize next sign; } what to do while true condition to test what to do while true how to stop

CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science While Loops and User Control Many while loops have a definitive ending specified in the condition programmed by the programmer (i.e. – counters)Many while loops have a definitive ending specified in the condition programmed by the programmer (i.e. – counters) However, most of the time we need code to repeat until the user (not the programmer) decides when it’s time to quitHowever, most of the time we need code to repeat until the user (not the programmer) decides when it’s time to quit You can write a program like this using a combination of while and if/else structuresYou can write a program like this using a combination of while and if/else structures

CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science Integrating While & If Structures Code Example 1 Code Example 2

CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science The “do/while” Loop Allows repeatable code while a given condition is true.Allows repeatable code while a given condition is true. Useful when the program should go on for an indeterminate length of timeUseful when the program should go on for an indeterminate length of time Must have a way of terminating the structure from within the loop!Must have a way of terminating the structure from within the loop! Post-Test Loop (Loop always executes AT LEAST ONCE)Post-Test Loop (Loop always executes AT LEAST ONCE)

CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science Basic Parts of a Do/While Loop The action to be performed AT LEAST ONCE and while the condition is evaluated to be trueThe action to be performed AT LEAST ONCE and while the condition is evaluated to be true The condition to testThe condition to test A method by which the condition can be evaluated to be false, ending the while loopA method by which the condition can be evaluated to be false, ending the while loop

CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science Do/While Loop – Everyday Example do { keep driving; recognize next sign; } while (sign is not a stop sign); what to do while true condition to test how to stop

CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science The “for” Loop AKA – The Counting Loop OR Counter-Controlled LoopAKA – The Counting Loop OR Counter-Controlled Loop Allows repeatable code until in an iterative loop for a finite amount of times.Allows repeatable code until in an iterative loop for a finite amount of times. Useful when the program runs for a pre- determined length of time.Useful when the program runs for a pre- determined length of time. Must have a way of terminating the structure from within the loop’s header (test against an maximum or minimum value).Must have a way of terminating the structure from within the loop’s header (test against an maximum or minimum value).

CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science Parts of a For Loop The conditions for the testThe conditions for the test –Starting point for counter –A maximum/minimum value –A way to increment/decrement the counter The action the loop should perform while the counter hasn’t met the max/min.The action the loop should perform while the counter hasn’t met the max/min.

CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science For Loop – Example for(var i=0; i<100; i++) { keep driving; } what to do when i<100 Starting Point Maximum Value How to Increment

CSCI N201: Programming Concepts Copyright ©2004  Department of Computer & Information Science Questions?