Web Programming– UFCFB Lecture 16

Slides:



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

CSCI N201: Programming Concepts Copyright ©2005  Department of Computer & Information Science Introducing JavaScript Loops.
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.
Executes a statement or statements for a number of times – iteration. Syntax for(initialize; test; increment) { // statements to be executed } Initial.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Loops – While, Do, For Repetition Statements Introduction to Arrays
CS1061: C Programming Lecture 8: Repetition A. O’Riordan, 2004.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
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.
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.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
CPS120 Introduction to Computer Science Iteration (Looping)
ㅎㅎ logical operator if if else switch while do while for Third step for Learning C++ Programming Repetition Control Structures.
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.
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.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
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.
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.
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.
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!
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.
Follow up from lab See Magic8Ball.java Issues that you ran into.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 10P. 1Winter Quarter Repetition Structures Lecture 10.
PHP Condtions and Loops Prepared by Dr. Maher Abuhamdeh.
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
Repetition Structures Chapter 9
Statements (6 of 6) A statement causes an action to be performed by the program. It translates directly into one or more executable computer instructions.
CiS 260: App Dev I Chapter 4: Control Structures II.
Ch 7: JavaScript Control Statements I.
JavaScript: Control Statements.
JavaScript: Control Statements I
Lecture 07 More Repetition Richard Gesick.
Control Structure Senior Lecturer
LESSON 11 – WHILE LOOPS UNIT 5 – 1/10/17.
Looping and Repetition
Programming Fundamentals Lecture #6 Program Control
MSIS 655 Advanced Business Applications Programming
Outline Altering flow of control Boolean expressions
Understanding the Three Basic Structures
Introduction to Problem Solving and Control Statements
CMPT 102 Introduction to Scientific Computer Programming
Chapter 6: Repetition Statements
Control Structures Part 1
Let’s all Repeat Together
Introduction to Repetition Structures
ICT Programming Lesson 3:
Using Decision Structures
Seating “chart” Front - Screen rows Back DOOR.
FLUENCY WITH INFORMATION TECNOLOGY
Instructor: Craig Duckett
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.
Looping and Repetition
Presentation transcript:

Web Programming– UFCFB3-30-1 Lecture 16 Introducing JavaScript Loops Web Programming– UFCFB3-30-1 Lecture 16 Instructor : Mazhar H Malik Email : mazhar@gcet.edu.om Global College of Engineering and Technology

Goals Understand how to create while loops in JavaScript. Understand how to create do/while loops in JavaScript. Understand how to create simple for (counting) 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 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 until a given condition is met Useful when the program should go on for an indeterminate length of time Must have a way of terminating the structure from within the loop! Pre-Test Loop (Loop may NEVER execute)

3 Basic Parts of a While Loop The condition being tested The 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 loop

While Loop – Everyday Example while (sign is not a stop sign) { keep driving; recognize next sign; } condition to test what to do while true what to do while true how to stop

While Loops and User Control 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 quit You can write a program like this using a combination of while and if/else structures

The “do/while” Loop Allows repeatable code while a given condition is true. Useful when the program should go on for an indeterminate length of time Must have a way of terminating the structure from within the loop! Post-Test Loop (Loop always executes AT LEAST ONCE)

Basic Parts of a Do/While Loop The action to be performed AT LEAST ONCE and while the condition is evaluated to be true The condition to test A method by which the condition can be evaluated to be false, ending the while loop

Do/While Loop – Everyday Example do { keep driving; recognize next sign; } while (sign is not a stop sign); what to do while true how to stop condition to test

The “for” Loop AKA – The Counting Loop OR Counter-Controlled Loop 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. Must have a way of terminating the structure from within the loop’s header (test against an maximum or minimum value).

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

for(var i=0; i<100; i++) { keep driving; } For Loop – Example Maximum Value for(var i=0; i<100; i++) { keep driving; } How to Increment Starting Point what to do when i<100

Questions?