Barbara Ericson Georgia Tech Sept 2005

Slides:



Advertisements
Similar presentations
ManipulatingPictures-Mod6-part21 Manipulating Pictures, Arrays, and Loops part 2 Barb Ericson Georgia Institute of Technology.
Advertisements

Georgia Institute of Technology Manipulating Pictures, Arrays, and Loops part 1.
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
CSE 113 Week 5 February , Announcements  Module 2 due 2/15  Exam 3 is on 2/15  Module 3 due 2/22  Exam 4 is on 2/25  Module 4 due 2/29.
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.
Intro-Sound-part21 Introduction to Processing Digital Sounds part 2 Barb Ericson Georgia Institute of Technology Oct 2009.
TOPIC 7 MODIFYING PIXELS IN A MATRIX NESTED FOR LOOPS 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by.
NestedLoops-part11 Nested Loops – part 1 Barb Ericson Georgia Institute of Technology Nov 2009.
CSC1401 Viewing a picture as a 2D image - 1. Review from the last week We used a getPixels() to get all of the pixels of a picture But this has been somewhat.
ManipulatingPictures-part11 Manipulating Pictures, Arrays, and Loops part 1 Barb Ericson Georgia Institute of Technology Nov 2009.
Georgia Institute of Technology Movies part 5 Barb Ericson Georgia Institute of Technology April 2006.
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
TOPIC 11 RETURNING VALUES FROM METHODS PICTURE TRANSFORMATIONS 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach.
Georgia Institute of Technology Manipulating Pictures, Arrays, and Loops Barb Ericson Georgia Institute of Technology August 2005.
TOPIC 6 MODIFYING PICTURES USING LOOPS 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and.
February ,  2/16: Exam 1 Makeup Papers Available  2/20: Exam 2 Review Sheet Available in Lecture  2/27: Lab 2 due by 11:59:59pm  3/2:
CPSC1301 Computer Science 1 Chapter 4 Manipulating Pictures, Arrays, and Loops part 5.
Conditionals-part21 Conditionals – part 2 Barb Ericson Georgia Institute of Technology Nov 2009.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
NestedLoops-part21 Nested Loops – part 2 Barb Ericson Georgia Institute of Technology Nov 2009.
Georgia Institute of Technology Manipulating Pictures, Arrays, and Loops Barb Ericson Georgia Institute of Technology August 2005.
CSE8A Lecture 5 TODO: –FINISH PSA2 WITH YOUR PARTNER! Read next class: Section 5.1. PLAY WITH CODE! –Get stuck, then figure out how to get unstuck – it’s.
Intro-Sound-Mod10-part31 Introduction to Processing Digital Sounds part 3 while loop, tracing, for loop, parameters Barb Ericson Georgia Institute of Technology.
ManipulatingPictures-part31 Manipulating Pictures, Arrays, and Loops part 3 Barb Ericson Georgia Institute of Technology Nov 2009.
04-ManipulatingPictures-part21 Manipulating Pictures, Arrays, and Loops part 2 Barb Ericson Georgia Institute of Technology June 2008.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS 1 Copyright: 2015 Illinois Institute of Technology_ George Koutsogiannakis.
Georgia Institute of Technology Two-Dimensional Arrays and Nested Loops – part 2 Barb Ericson Georgia Institute of Technology August 2005.
Manipulating Pictures, Arrays, and Loops part 2
Manipulating Pictures, Arrays, and Loops part 2
Topic 6 Modifying Pictures Using Loops
Manipulating Pictures, Arrays, and Loops part 3
Workshop for Programming And Systems Management Teachers
Chapter 5 Repetition.
Manipulating Pictures, Arrays, and Loops part 2
Manipulating Pictures, Arrays, and Loops part 2
Working with ranges in pictures
For Loops October 12, 2017.
Manipulating Pictures, Arrays, and Loops
Repeating Instructions And Advance collection
Two-Dimensional Arrays and Nested Loops – part 1
Introduction to Processing Digital Sounds part 2
Manipulating Pictures, Arrays, and Loops part 5
In this class, we will cover:
Two-Dimensional Arrays and Nested Loops – part 1
Looping through pixels.
CSE 8A Lecture 6 Reading for next class:
Manipulating Pictures, Arrays, and Loops
Manipulating Pictures, Arrays, and Loops part 3
Barb Ericson Georgia Institute of Technology June 2006
Introduction to Processing Digital Sounds part 3
Lab5 PROGRAMMING 1 Loop chapter4.
Building Java Programs
Two-Dimensional Arrays and Nested Loops – part 2
Manipulating Pictures, Arrays, and Loops
Manipulating Pictures, Arrays, and Loops
Manipulating Pictures, Arrays, and Loops
Manipulating Pictures, Arrays, and Loops part 6
OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS
February , 2009 CSE 113 B.
In this class, we will cover:
Manipulating Pictures, Arrays, and Loops
PROGRAM FLOWCHART Iteration Statements.
The for-statement.
Building Java Programs
CSC1401 Manipulating Pictures 2
Barb Ericson Georgia Institute of Technology April 2006
Manipulating Pictures, Arrays, and Loops part 6
Looping and Repetition
Presentation transcript:

Barbara Ericson Georgia Tech Sept 2005 Loops - Review Barbara Ericson Georgia Tech Sept 2005 Georgia Institute of Technology

Georgia Institute of Technology Purpose of Loops Repeat execution of a single statement or block of code Often with at least one variable changing each time through the loop If I ask you to print out “I love Java!” 100 times Would you want to type 100 statements of System.out.println(“I love Java!”); Georgia Institute of Technology

Georgia Institute of Technology Types of Loops For each loop (1.5 or 5.0) Do body of loop for each element in a collection An Array, List, or Set While loop Loop while a boolean test is true Declare and initialize variables before the test Change loop variable in the body of the loop For loop The same as the while loop but with one place to Declare and initialize variables Specify the boolean test Change variables Georgia Institute of Technology

Georgia Institute of Technology For Each Loop Syntax for (type name : arrayName) { // do these statements for each element } Georgia Institute of Technology

Georgia Institute of Technology Decrease Red – For Each public void decreaseRed() { Pixel[] pixelArray = this.getPixels(); int value = 0; // loop through all the pixels for (Pixel pixelObj : pixelArray) // get the red value value = pixelObj.getRed(); // decrease the red value value = (int) (value * 0.5); // set the red value pixelObj.setRed(value); } Georgia Institute of Technology

Georgia Institute of Technology While Loop Syntax while (test) { // statements to repeat while test is true } Georgia Institute of Technology

Georgia Institute of Technology While Loop Example // declare variables int count = 0; // check if should do body while (count < 100) { System.out.println(“I love Java”); count = count + 1; // change variables } Georgia Institute of Technology

Georgia Institute of Technology Decrease Red - While public void decreaseRed() { Pixel[] pixelArray = this.getPixels(); Pixel pixel = null; int value = 0; int index = 0; // loop through all the pixels while(index < pixelArray.length) // get the current pixel pixel = pixelArray[index]; // get the value value = pixel.getRed(); // decrease the red value value = (int) (value * 0.5); // set the red value pixel.setRed(value); // increment the index index = index + 1; } Georgia Institute of Technology

Georgia Institute of Technology For Loop Syntax for (declare and init; test; change) { // statements to execute while test is true } Georgia Institute of Technology

Georgia Institute of Technology Decrease Red - For public void decreaseRedFor() { Pixel[] pixelArray = this.getPixels(); Pixel pixel = null; int value = 0; // loop through all the pixels for (int i = 0; i < pixelArray.length; i++) // get the current pixel pixel = pixelArray[i]; // get the value value = pixel.getRed(); // decrease the red value value = (int) (value * 0.5); // set the red value pixel.setRed(value); } Georgia Institute of Technology

Georgia Institute of Technology Nested Loops Put one loop inside of the block of another Useful for walking through 2-dimensional arrays Outer loop for rows Inner loop for columns Useful for keeping track of the row and column values For use in the loop Georgia Institute of Technology

Georgia Institute of Technology Nested Loop Example public void lighten() { Pixel pixel = null; Color color = null; // loop through the columns for (int x = 0; x < getWidth(); x++) // loop through the rows for (int y = 0; y < getHeight(); y++) // get pixel at the x and y location pixel = getPixel(x,y); // get the current color color = pixel.getColor(); // get a lighter color color = color.brighter(); // set the pixel color pixel.setColor(color); } Georgia Institute of Technology

Georgia Institute of Technology Mirror Vertical public void mirrorVertical() { int width = this.getWidth(); int mirrorPoint = width / 2; Pixel leftPixel = null; Pixel rightPixel = null; // loop through all the rows for (int y = 0; y < getHeight(); y++) // loop from 0 to the middle (mirror point) for (int x = 0; x < mirrorPoint; x++) leftPixel = getPixel(x, y); rightPixel = getPixel(width - 1 - x, y); rightPixel.setColor(leftPixel.getColor()); } Georgia Institute of Technology

Georgia Institute of Technology Ways to Teach This Ask students to repeat some action for a set number of times Jump up and down Clap How do we know if they did it right? Walk through what is happening with a nested loop Each student do the next time through the loop Georgia Institute of Technology