Using Loops. Goals Understand how to create while loops in JavaScript. Understand how to create do/while loops in JavaScript. Understand how to create.

Slides:



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

Week 5: Loops 1.  Repetition is the ability to do something over and over again  With repetition in the mix, we can solve practically any problem that.
CS0004: Introduction to Programming Repetition – Do Loops.
CSE 1301 Lecture 6B More Repetition Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
Repeating Actions While and For Loops
Computer Science 1620 Loops.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 5 Looping.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 5: Looping by Tony.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
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 (!)
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
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.
Control Structures - Repetition Chapter 5 2 Chapter Topics Why Is Repetition Needed The Repetition Structure Counter Controlled Loops Sentinel Controlled.
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);
Programming with C# Iteration LECTURE 3. Summary of last lecture SequenceSelectionif and switch statementsCastingRandom.
CPS120 Introduction to Computer Science Iteration (Looping)
Chapter 4: Loops and Files
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
CPS120: Introduction to Computer Science Decision Making in Programs.
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
C++ for Engineers and Scientists, Third Edition1 Objectives In this chapter, you will learn about: Basic loop structures while loops Interactive while.
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,
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 5: Introduction to C: More Control Flow.
Logic Our programs will have to make decisions on what to do next –we refer to the decision making aspect as logic Logic goes beyond simple if and if-else.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 ‏ Control Structures.
CONTENTS Processing structures and commands Control structures – Sequence Sequence – Selection Selection – Iteration Iteration Naming conventions – File.
Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
Lecture 4: C/C++ Control Structures Computer Programming Control Structures Lecture No. 4.
Structured Program Development Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 5: Looping.
ITERATIVE STATEMENTS. Definition Iterative statements (loops) allow a set of instruction to be executed or performed several until condition are met.
Pascal Programming Iteration (looping) Carl Smith National Certificate Unit 4.
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.
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.
© The McGraw-Hill Companies, 2006 Chapter 3 Iteration.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
Repetition Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Intro to Loops 1.General Knowledge 2.Two Types of Loops 3.The WHILE loop 1.
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: Repetition Structure (Loop) Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills.
While loops. Iteration We’ve seen many places where repetition is necessary in a problem. We’ve been using the for loop for that purpose For loops are.
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.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
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.
REPETITION CONTROL STRUCTURE
Unit 3 Lesson 9 Repetition Statements (Loops)
Review If you want to display a floating-point number in a particular format use The DecimalFormat Class printf A loop is… a control structure that causes.
Lecture 07 More Repetition Richard Gesick.
Web Programming– UFCFB Lecture 16
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
Scratch: selection / branching/ if / If…else / compound conditionals / error trapping by Mr. Clausen.
Structured Program
Introduction to Problem Solving and Control Statements
Chapter 6: Repetition Statements
ICT Programming Lesson 3:
Using Decision Structures
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:

Using Loops

Goals Understand how to create while loops in JavaScript. Understand how to create do/while loops in JavaScript. Understand how to create simple for loops in JavaScript.

Computing Structures 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 (Iterative) Structures – Today’s unit. ◦ Decision (Selection) Structures – those structures where code alternate executes based on some Boolean test.

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. 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).

Parts of a Loop 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.

Conditional Loops 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 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.

The while Loop Allows repeatable code so long as a condition is evaluated to be TRUE. Terminates when something happens that causes the condition to be evaluated as FALSE. Useful when the program should go on for an indeterminate length of time (you, as the programmer, don’t know when the loop will end). It is a pre-test loop; there is a chance it may never execute!

While Loop Structure while (condition to be tested) { executable block that will repeat if condition is TRUE }

While Loop – Code Examples Simple While – Example 1 Simple While – Example 2

While Loops and User Control Many while loops have a definitive ending specified in the condition programmed by the programmer (i.e. – counters). However, sometimes we need code to repeat until the user (not the programmer) decides when it’s time to quit. This can be done using a combination of while and alternative (if-then; if-then-else) structures.

Combining While & If Structures Code Example

Data Validation We can also use a while loop to enforce validation of data the user enters. Let’s say your creating the previous grade program and that you want the user to enter data that “fits” in the following ranges: ◦ All user-entered data must be of number type (A) ◦ All user-entered data must be less than 100, inclusive (B) ◦ All user-entered data must be greater than 0, inclusive (C)

Data Validation We already have one loop, to take care of allowing the user to enter data until they enter “-1” (which we will keep). Let’s add a nested loop to take care of the validation. But how will we construct our condition? Remember that, we are actually testing 3 conditions …

Data Validation We’ll created a compound condition, using Boolean operators … Our 3 conditions: ◦ All user-entered data must be of number type (A) ◦ All user-entered data must be less than 100, inclusive (B) ◦ All user-entered data must be greater than 0, inclusive (C) How do we want to construct our compound condition? ◦ Do we want to force the user to re-enter if the user fails all three tests? ◦ Or do we want to force the user to re-enter if the user fails any one of the tests?

Data Validation ABCA&&B A&&B &&C A||B A||B ||C FFFFFFF FFTFFFT FTFFFTT FTTFFTT TFFFFTT TFTFFTT TTFTFTT TTTTTTT

Testing for Numeric Data Okay, testing to see that data is greater than 100 or less than 0 is easy, but how do we test for numeric data? We can use a function called NaN() NaN() takes any type of data and tests to see if that data can be converted to a numeric type – integer or float. If the data cannot be converted, the NaN() ( NaN – “Not a Number”) returns a True value.

Data Validation Code Example

The do while Loop Very similar to a while loop: ◦ Allows repeatable code so long as a condition is evaluated to be TRUE. ◦ Terminates when something happens that causes the condition to be evaluated as FALSE. ◦ Useful when the program should go on for an indeterminate length of time (you, as the programmer, don’t know when the loop will end). It is a post-test loop; it will always execute at least once!

Do While Loop Structure do { executable block that will repeat if condition is TRUE } while (condition to be tested);

Do While Loop - Example Code Example

The For Loop The for loop is an iterative loop. Repeats a code block until the loop reaches a maximum or minimum value. It is useful when the program should run on for a pre-determined length of time. Derived from while loops with counters.

Parts of a For Loop The loop conditions: ◦ A starting number for the counter (usually 1 or 0) ◦ A maximum or minimum value ◦ A way to increment/decrement the counter The executable block that will execute so long as the maximum/minimum value is not yet met.

For Loop Structure for(A; B; C) { executable block; } KEY: A – Counter’s starting value B – Test again a maximum or minimum value C – How to increment/decremen t the counter. KEY: A – Counter’s starting value B – Test again a maximum or minimum value C – How to increment/decremen t the counter.

For Loop Example Code Example

Questions?