Recognizing Patterns Counting: continually updating a value by a fixed amount Counting raindrops int dropCount = 0; //Raindrop counter while (dropCount.

Slides:



Advertisements
Similar presentations
Chapter 7 - Iteration. Chapter Goals Program repitiation statements – or loops – with the for, while, and do-while statements Program repitiation statements.
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.
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.
Introduction to Computers and Programming Lecture 9: For Loops New York University.
COMP 14 Introduction to Programming Miguel A. Otaduy May 21, 2004.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-2: The for Loop reading: 2.3 self-check: exercises: 2-14 videos:
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Chapter 5: Control Structures II (Repetition)
Loops – While, Do, For Repetition Statements Introduction to Arrays
Loops –For For Reading for this Lecture, L&L, Part of 5.8.
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Flow of Control Loops – Chapter 3.2. Java Loop Statements: Outline the while Statement the do-while Statement the for Statement.
Chapter 5: Repetition Statements. In this chapter, you will learn about: Basic loop structures while loops Interactive while loops for loops Loop programming.
Copyright © Texas Education Agency, Computer Programming For Loops.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
Do … while ( continue_cond ) Syntax: do { stuff you want to happen in the loop } while (continue_condition);
Copyright 2009 by Pearson Education Building Java Programs Chapter 2 Lecture 2-2: The for Loop reading: 2.3 self-check: exercises: 2-14 videos: Ch.
CS303ELoops1 CS 303E Lecture 8: Loops Self-referentially, short for GNU's not UNIX, a Unix-compatible software system developed by the Free Software Foundation.
C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved For Loops October 16, 2013 Slides by Evan Gallagher.
Chapter 6: Iteration Part 1. To be able to program loops with the while, for, and do statements To avoid infinite loops and off-by-one errors To understand.
Loops: Handling Infinite Processes CS 21a: Introduction to Computing I First Semester,
Java Loops (Java: An Eventful Approach, Ch 7 and 13), Slides Credit: Bruce, Danyluk and Murtagh CS 120 Lecture 16 6 November 2012.
Loops and Iteration for Statements, while Statements and do-while Statements.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Repetition Statements.  Often it is necessary to repeat statements many times  Java has two ways of doing this  while statements  for statements.
C++ for Engineers and Scientists, Third Edition1 Objectives In this chapter, you will learn about: Basic loop structures while loops Interactive while.
Repetition Structures Repetition Structures allow you to write programs that will repeat program steps multiple times. –Also called Loops –Counter controlled.
Repetition. Control of Flow SEQUENCE SELECTION (if..else, switch…case) REPETITION.
1 BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA AND DEFINITE LOOPS.
Recognizing Patterns Counting: continually updating a value by a fixed amount Counting raindrops int dropCount = 0; //Raindrop counter while (dropCount.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-2: The for Loop reading: 2.3 self-check: exercises: 2-14 videos:
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-2: The for Loop reading: 2.3 self-check: exercises: 2-14 videos:
Repetition Statements (Loops) The do while Loop The last iteration structure in C++ is the do while loop. A do while loop repeats a statement or.
Repetition Control Structure. Introduction Many applications require certain operations to be carried out more than once. Such situations require repetition.
Building Java Programs Chapter 2 Primitive Data and Definite Loops Copyright (c) Pearson All rights reserved.
Chapter 6 - Repetition. while Loop u Simplest loop u Two parts: test expression and loop body u Pre-tested loop –Execute loop body if test true –Bypass.
1 Week 9 Loops. 2 Repetition: Loops l Structure: »Usually some initialization code »body of loop »loop termination condition l Several logical organizations.
While ( number
Catie Welsh February 9,  Friday - No Lab! ◦ Bring questions on Project 2  Lab 3 due on Friday 2.
Loops. More Flow of Control  Sometimes we want to do something many times.  Don’t have to write all the steps out multiple times.  Use a LOOP – control.
Loops Tonga Institute of Higher Education. Introduction Programs need to be able to execute tasks repeatedly. Use loops to repeat actions  For Loop 
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
Chapter 5: Loops Tarik Booker CS 201 California State University, Los Angeles.
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 :
Identify the Appropriate Method for Handling Repetition
Slides by Evan Gallagher
Chapter 4 Repetition Statements (loops)
REPETITION CONTROL STRUCTURE
Department of Computer Science
Loop Structures.
Chapter 5 Repetition.
Looping and Repetition
Iteration with While You can say that again.
Outline Altering flow of control Boolean expressions
Introduction to Object-Oriented Programming with Java--Wu
LOOPS BY: LAUREN & ROMEO.
3.5- The while Statement The while statement has the following syntax:
Chapter 6: Repetition Statements
Loop Strategies Repetition Playbook.
Building Java Programs
Building Java Programs
PROGRAM FLOWCHART Iteration Statements.
Building Java Programs
Chapter 3 Flow of Control Loops in Java.
Looping and Repetition
Presentation transcript:

Recognizing Patterns Counting: continually updating a value by a fixed amount Counting raindrops int dropCount = 0; //Raindrop counter while (dropCount < MAX) { new Raindrop( … ); dropCount++; }

Counting Bricks while ( count < TOTAL ) { new Brick(…); count++; }

The Counting while Loop int i = initialValue;// initialize while (i < stopVal) { // test... // do stuff i++; // increment }

The for loop Especially useful for counting Ex: for ( int i=initialVal; //initialize i<stopVal; //test i++;) {//increment …//do stuff }

Counting Raindrops with for Loop for (int dropCount = 0; dropCount <MAX; dropCount++) { new Raindrop ( … ); }

More General Start and End Points Loops can take whatever starting point, end point, and increment Ex: for (int i=23; i <= 1728; i=i+591;){ //do stuff } But one should avoid using a double for any of the three values

Counting Backwards with for Loop Ex: Printing a countdown for (int count = 10; count >= 1; count--) { System.out.println(count); }

Update Values Can increment loop index by any value Ex: Drawing grass blades for (int pos = 0; pos < WIDTH; pos = pos + GAP) { new Line (pos, TOP, pos, GROUND, canvas); }

General Syntax of for Loop for (initialization; condition; update) { //Do something } Initialization: gen’ly creates a counting variable Condition: a boolean expression to stop the loop Updating: updates the variable created

Nested Loops Any loop body can contain another loop Ex: for ( … ) { while (…) { for(…) { }

The do while Loop Syntax: do { } while ( ) Craps Example

do while Loop vs while Loop do while –Condition checked at the end –Loop body executed at least once while –Condition checked at the beginning –Loop body may never execute

Avoiding Loop Errors Easier to find errors if you know where to look Common loop errors include: Off by 1 in counting loops Infinite loops

Off by one errors Suppose we want to run a for loop 5 times: The left hand version will run it 6 times, not 5. for(int i=0;i<=5; i++){ } for(int i=0;i<5;i++) { }

Infinite Loops Ex: while ( count< TOTAL ) { new Brick (…); } Since value of count is not updated, the condition in while will stay true forever.