Presentation is loading. Please wait.

Presentation is loading. Please wait.

Barbara Ericson Georgia Tech Sept 2005

Similar presentations


Presentation on theme: "Barbara Ericson Georgia Tech Sept 2005"— Presentation transcript:

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

2 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

3 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

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

5 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

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

7 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

8 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

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

10 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

11 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

12 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

13 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 x, y); rightPixel.setColor(leftPixel.getColor()); } Georgia Institute of Technology

14 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


Download ppt "Barbara Ericson Georgia Tech Sept 2005"

Similar presentations


Ads by Google