1 Looping Dale/Weems/Headington. 2 KA/JS/P Warning l Save your work often! l In the Khan Academy, JavaScript environment, infinite loops will lock up.

Slides:



Advertisements
Similar presentations
Repetition Statements Perform the same task repeatedly Allow the computer to do the tedious, boring things.
Advertisements

CS0004: Introduction to Programming Repetition – Do Loops.
LOOP / REPETITION while loop. for loop do/while loop We assume that loops are not meant to be infinite. That is, there should always be a way out of the.
Executes a statement or statements for a number of times – iteration. Syntax for(initialize; test; increment) { // statements to be executed } Initial.
Computer Science 1620 Loops.
COMP 14 Introduction to Programming Miguel A. Otaduy May 21, 2004.
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
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.
Repetition Statements repeat block of code until a condition is satisfied also called loops Java supports 3 kinds of loops: while statement – repeats a.
1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.
1 Chapter 6 Looping Dale/Weems/Headington. 2 l Physical order vs. logical order l A loop is a repetition control structure based on a condition. l it.
1 Chapter 5 File Objects and Looping Statements (Some slides have been modified from their original format)
New Mexico Computer Science For All More Looping in NetLogo Maureen Psaila-Dombrowski.
CHAPTER 5 CONTROL STRUCTURES II (Repetition). In this chapter, you will:  Learn about repetition (looping) control structures  Explore how to construct.
Fundamentals of Python: From First Programs Through Data Structures
Fundamentals of Python: First Programs
1 What is a loop? A loop is a repetition control structure that causes a single statement or block to be executed repeatedly Loops.
Flow of Control. 2 Control Structures Control structure: An instruction that determines the order in which other instructions in a program are executed.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
1 Chapter 9 Additional Control Structures Dale/Weems/Headington.
Chapter 5 Control Structures: Loops 5.1 The while Loop The while loop is probably the most frequently used loop construct. The while loop is a conditional.
WDMD 170 – UW Stevens Point 1 WDMD 170 Internet Languages eLesson: Decision Making with Control Structures and Statements (non-audio version) © Dr. David.
Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP For loop is a counter controlled loop. For loop is a pretest loop. Used when number.
Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using.
1 Chapter 9 Additional Control Structures Dale/Weems.
1 Additional Control Structures. 2 Chapter 9 Topics  Switch Statement for Multi-way Branching  Do-While Statement for Looping  For Statement for Looping.
1 Do-While Statement Is a looping control structure in which the loop condition is tested after each iteration of the loop. SYNTAX do { Statement } while.
Chapter 7 Additional Control Structures. 2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response.
Chapter 5 Loops. Overview u Loop Statement Syntax  Loop Statement Structure: while, for, do-while u Count-Controlled Loops u Nested Loops u Loop Testing.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
Chapter 6 Looping CS185/09 - Introduction to Programming Caldwell College.
Program Flow Control - Looping Addis Ababa Institute of Technology Yared Semu April 2012.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Overview Go over parts of quiz? Another iteration structure for loop.
 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 do-while Statement 2 Do-While Statement Is a looping control structure in which the loop condition is tested after each iteration of the loop. SYNTAX.
In the last lesson we discussed about: Casting Precedence The “=“ used as an assignment operator Made a calculate average program.
Repetition Control Structure. Introduction Many applications require certain operations to be carried out more than once. Such situations require repetition.
1 For Loops l From Chapter 9 l A shorthand way of coding count loops.
REPETITION STATEMENTS - Part1  Also called LOOP STATEMENTS OR LOOP STRUCTURES 1 C++ Statements that repeat one or more actions while some condition is.
1 Programming in C++ Dale/Weems/Headington Chapter 9 Additional Control Structures (Switch, Do..While, For statements)
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.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
1 Programming in C++ Dale/Weems/Headington Chapter 6 Looping.
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
Catie Welsh February 9,  Friday - No Lab! ◦ Bring questions on Project 2  Lab 3 due on Friday 2.
Sesi 0607EKT120/4 Computer Programming Week 5 – Repetition / Loops.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Chapter 6. Loops A control structure that causes a statement or group of statements to be executed repeatedly There are 3 types of loops –while –for –do.
Chapter 6 Looping. 2 l A loop is a repetition control structure. l it causes a single statement or block to be executed repeatedly What is a loop?
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.
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
Topic : While, For, Do-While Loop Guided By : Branch : Batch :
Chapter 6: Loops.
Chapter 4 Repetition Statements (loops)
Lecture 6 Repetition Richard Gesick.
Loops in Java.
ITM 352 Flow-Control: Loops
Logical Operators and While Loops
Lecture 4A Repetition Richard Gesick.
Iteration with While You can say that again.
Additional Control Structures
Repetition Control Structure
Lab5 PROGRAMMING 1 Loop chapter4.
Logical Operators and While Loops
The structure of programming
PROGRAM FLOWCHART Iteration Statements.
Python While Loops.
REPETITION Why Repetition?
Presentation transcript:

1 Looping Dale/Weems/Headington

2 KA/JS/P Warning l Save your work often! l In the Khan Academy, JavaScript environment, infinite loops will lock up the browser.

3 l A loop is a repetition control structure. l it causes a single statement or block to be executed repeatedly What is a loop?

4 While Statement SYNTAX while ( Expression ) {.. // loop body. } NOTE: Loop body can be a null statement, or a block.

5 When the expression is tested and found to be false, the loop is exited and control passes to the statement which follows the loop body. WHILE LOOP FALSE TRUE body statement Expression

6 an initialization of the loop control variable an expression to test for continuing the loop an update of the loop control variable to be executed with each iteration of the body Count-controlled loop contains

7 var count ; count = 4; // initialize loop variable while (count > 0) // test expression { println(count); // repeated action count -- ; // update loop variable } println("Done"); Count-controlled Loop

8 var count ; count = 4; while (count > 0) { println(count); count -- ; } println("Done"); OUTPUT count

9 Count-controlled Loop var count ; count = 4; while (count > 0) { println(count); count -- ; } println("Done"); OUTPUT count 4

10 Count-controlled Loop var count ; count = 4; while (count > 0) TRUE { println(count); count -- ; } println("Done"); OUTPUT count 4

11 Count-controlled Loop var count ; count = 4; while (count > 0) { println(count); count -- ; } println("Done"); OUTPUT 4 count 4

12 Count-controlled Loop var count ; count = 4; while (count > 0) { println(count); count -- ; } println("Done"); OUTPUT 4 count 3

13 Count-controlled Loop var count ; count = 4; while (count > 0) TRUE { println(count); count -- ; } println("Done"); OUTPUT 4 count 3

14 Count-controlled Loop var count ; count = 4; while (count > 0) { println(count); count -- ; } println("Done"); OUTPUT 4 3 count 3

15 Count-controlled Loop var count ; count = 4; while (count > 0) { println(count); count -- ; } println("Done"); OUTPUT 4 3 count 2

16 Count-controlled Loop var count ; count = 4; while (count > 0) TRUE { println(count); count -- ; } println("Done"); OUTPUT 4 3 count 2

17 Count-controlled Loop var count ; count = 4; while (count > 0) { println(count); count -- ; } println("Done"); OUTPUT count 2

18 Count-controlled Loop var count ; count = 4; while (count > 0) { println(count); count -- ; } println("Done"); OUTPUT count 1

19 Count-controlled Loop var count ; count = 4; while (count > 0) TRUE { println(count); count -- ; } println("Done"); OUTPUT count 1

20 Count-controlled Loop var count ; count = 4; while (count > 0) { println(count); count -- ; } println("Done"); OUTPUT count 1

21 Count-controlled Loop var count ; count = 4; while (count > 0) { println(count); count -- ; } println("Done"); OUTPUT count 0

22 Count-controlled Loop var count ; count = 4; while (count > 0) FALSE { println(count); count -- ; } println("Done"); OUTPUT count 0

23 Count-controlled Loop var count ; count = 4; while (count > 0) { println(count); count -- ; } println("Done"); OUTPUT Done count 0

24 for Statement

25 A Count-Controlled Loop SYNTAX for ( initialization ; test expression ; update ) { 0 or more statements to repeat }

26 The for loop contains an initialization an expression to test for continuing an update to execute after each iteration of the body

27 Example of Repetition for ( var num = 1 ; num <= 3 ; num++ ) { println(num + " Potato"); }

28 Example of Repetition num var num; for ( num = 1 ; num <= 3 ; num++ ) { println(num + " Potato"); OUTPUT ?

29 Example of Repetition num OUTPUT 1 var num; for ( num = 1 ; num <= 3 ; num++ ) { println(num + " Potato");

30 Example of Repetition num OUTPUT 1 var num; for ( num = 1 ; num <= 3 ; num++ ) { println(num + " Potato"); true

31 Example of Repetition num var num; for ( num = 1 ; num <= 3 ; num++ ) { println(num + " Potato"); OUTPUT 1 1Potato

32 Example of Repetition num OUTPUT 2 var num; for ( num = 1 ; num <= 3 ; num++ ) { println(num + " Potato"); 1Potato

33 Example of Repetition num OUTPUT 2 true 1Potato var num; for ( num = 1 ; num <= 3 ; num++ ) { println(num + " Potato");

34 Example of Repetition num var num; for ( num = 1 ; num <= 3 ; num++ ) { println(num + " Potato"); OUTPUT 2 1Potato 2Potato

35 Example of Repetition num OUTPUT 3 var num; for ( num = 1 ; num <= 3 ; num++ ) { println(num + " Potato"); 1Potato 2Potato

36 Example of Repetition num OUTPUT 3 true 1Potato 2Potato var num; for ( num = 1 ; num <= 3 ; num++ ) { println(num + " Potato");

37 Example of Repetition num var num; for ( num = 1 ; num <= 3 ; num++ ) { println(num + " Potato"); OUTPUT 3 1Potato 2Potato 3Potato

38 Example of Repetition num OUTPUT 4 var num; for ( num = 1 ; num <= 3 ; num++ ) { println(num + " Potato"); 1Potato 2Potato 3Potato

39 Example of Repetition num OUTPUT 4 false 1Potato 2Potato 3Potato var num; for ( num = 1 ; num <= 3 ; num++ ) { println(num + " Potato");

40 Example of Repetition num When the loop control condition is evaluated and has value false, the loop is said to be “satisfied” and control passes to the statement following the For statement. 4 false var num; for ( num = 1 ; num <= 3 ; num++ ) { println(num + " Potato");

41 The output was: 1Potato 2Potato 3Potato

42 for (var count = 4 ; count > 0 ; count -- ) { println(count); } println(“Done”); Count-controlled Loop OUTPUT: Done

Count Control Loop Example Display integers and their squares from 1 through 10. for (var i = 1; i <= 10; i++) { println(i + " " + i*i); }

For example Display even integers and their squares from 1 through 10. for (var i = 2; i <= 10; i = i+2) { println(i + " " + i*i); }

For example Display integers and their squares from 10 down to 1. for (var i = 10; i >= 1; i--) { println(i + " " + i*i); }

For example Find square roots of 1.1, 1.2, 1.3,..., 2.0 for (var x = 1.1; x <= 2.0; x =x+0.1) { println(x + " " + sqrt(x)); }

Compute and return n! = 1  2  3 ...  n. var product = 1; for (var i = 2; i <= n; i++) { product = product * i; } For example