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

Slides:



Advertisements
Similar presentations
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.
Advertisements

Repeating Actions While and For Loops
Computer Science 1620 Loops.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 6, 2005.
COMP 14 Introduction to Programming Miguel A. Otaduy May 21, 2004.
1 Parts of a Loop (reminder) Every loop will always contain three main elements: –Priming: initialize your variables. –Testing: test against some known.
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:
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
Loops – While, Do, For Repetition Statements Introduction to Arrays
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Loops II Lecture 13, Thu Feb
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Loops III Lecture 19, Wed Mar
Recognizing Patterns Counting: continually updating a value by a fixed amount Counting raindrops int dropCount = 0; //Raindrop counter while (dropCount.
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.
An Object-Oriented Approach to Programming Logic and Design Chapter 6 Looping.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
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.
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.
Repetition Statements.  Often it is necessary to repeat statements many times  Java has two ways of doing this  while statements  for statements.
 Wednesday, 9/18/02, Slide #1 CS106 Introduction to CS1 Wednesday, 9/18/02  QUESTIONS?? HW #1 due today at 5!!  Today: Loops, and two new data types.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
CS101 Computer Programming I Chapter 4 Extra Examples.
Repetition. Control of Flow SEQUENCE SELECTION (if..else, switch…case) REPETITION.
Introduction to Loops Iteration Repetition Counting Loops Also known as.
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 8/9/2009. Learning to Program -- Suggestions Spend a lot of time fiddling around with code –Programming is something you have to learn by doing.
Building Java Programs Chapter 2 Primitive Data and Definite Loops Copyright (c) Pearson All rights reserved.
Nested for loops.
Printing with for Loops To print a character multiple times, use a for loop. for (int j = 1; j
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.
Flow of Control: Loops Module 4. Objectives Design a loop Use while, do, and for in a program Use the for-each with enumerations Use assertion checks.
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
Loops ( while and for ) CSE 1310 – Introduction to Computers and Programming Alexandra Stefan 1.
Chapter 5: Loops Tarik Booker CS 201 California State University, Los Angeles.
Slides by Evan Gallagher
Chapter 4 Repetition Statements (loops)
REPETITION CONTROL STRUCTURE
Loop Structures.
LOOPS.
Looping and Repetition
Iteration with While You can say that again.
Outline Altering flow of control Boolean expressions
LOOPS BY: LAUREN & ROMEO.
Building Java Programs
3.5- The while Statement The while statement has the following syntax:
Building Java Programs
Lab5 PROGRAMMING 1 Loop chapter4.
Building Java Programs
Variables variable: A piece of the computer's memory that is given a name and type, and can store a value. Like preset stations on a car stereo, or cell.
Loop Strategies Repetition Playbook.
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Chapter 2 Lecture 2-2: The for Loop reading: 2.3
Announcements Lab 3 was due today Assignment 2 due next Wednesday
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

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

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); }

Debugging Loops You may find the following useful to fix your loops when they are not working right: –Memory table (trace) –Inserting System.out.println( ) statements –Using the BlueJ debugger

Debugging using the Memory Table (Trace) method The purpose of this exercise is to help reinforce how loops actually work The computer follows a specific set of rules involving loops. Until you get an intuition for it, you may have to periodically trace a loop to understand how it works (or does not work) The code examples are arbitrary but reflect what comes up often in loops

Trace the following statements summkk< 5?

Trace the following statements Solution: summkk< 5? 0100T 121T 11142T 12163T 13184T 14205F done

Another trace example jj<=18?Console

Another trace example--SOLN jj<=18?Console 10T 13T T F

and another ii<4pp%2 == 0Console

and another SOLN ii<4pp%2 == 0Console 10 1T17F 2T24T17 24 even 3T31F17 24 even 31 4F(done)

Lab 13 Problem 8 Draw the row of windows first The size of the windows is 30 (or so) Separated by gives 450 total + edges Then draw the outline. Try to center row of windows in buiilding

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

Tracing a nested loop ii<4kk>2Console

Tracing a nested loop -- SOLN ii<4kk>2Console 0T5T5 4T4 3T3 2F 1T5T6 4T5 etc

Lab 13 Problem 14 sinitialize xpos, ypos sset up outer loop to go 0 to 19 { s set up inner loop 0 to 3 { draw a window change xpos } change ypos reset xpos }

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.