Loops Review Barbara Ericson Georgia Tech Sept 2005
Loops - Review Barbara Ericson Georgia Tech Sept 2005
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!”);
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
For Each Loop Syntax for (type name : array. Name) { // do these statements for each element }
Decrease Red – For Each public void decrease. Red() { Pixel[] pixel. Array = this. get. Pixels(); int value = 0; // get the red value = pixel. Obj. get. Red(); // decrease the red value = (int) (value * 0. 5); // loop through all the pixels for (Pixel pixel. Obj : pixel. Array) { // set the red value pixel. Obj. set. Red(value); } }
While Loop Syntax while (test) { // statements to repeat while test is true }
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 }
Decrease Red - While public void decrease. Red() { Pixel[] pixel. Array = this. get. Pixels(); Pixel pixel = null; int value = 0; int index = 0; // loop through all the pixels while(index < pixel. Array. length) { // get the current pixel = pixel. Array[index]; // get the value = pixel. get. Red(); // decrease the red value = (int) (value * 0. 5); // set the red value pixel. set. Red(value); // increment the index = index + 1; } }
For Loop Syntax for (declare and init; test; change) { // statements to execute while test is true }
Decrease Red - For public void decrease. Red. For() { Pixel[] pixel. Array = this. get. Pixels(); Pixel pixel = null; int value = 0; // loop through all the pixels for (int i = 0; i < pixel. Array. length; i++) { // get the current pixel = pixel. Array[i]; // get the value = pixel. get. Red(); // decrease the red value = (int) (value * 0. 5); // set the red value pixel. set. Red(value); } }
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
Nested Loop Example public void lighten() { Pixel pixel = null; Color color = null; // get pixel at the x and y location pixel = get. Pixel(x, y); // get the current color = pixel. get. Color(); // loop through the columns for (int x = 0; x < get. Width(); x++) { // loop through the rows for (int y = 0; y < get. Height(); y++) { // get a lighter color = color. brighter(); // set the pixel color pixel. set. Color(color); } } }
Mirror Vertical public void mirror. Vertical() { int width = this. get. Width(); int mirror. Point = width / 2; Pixel left. Pixel = null; Pixel right. Pixel = null; // loop through all the rows for (int y = 0; y < get. Height(); y++) { // loop from 0 to the middle (mirror point) for (int x = 0; x < mirror. Point; x++) { left. Pixel = get. Pixel(x, y); right. Pixel = get. Pixel(width - 1 - x, y); right. Pixel. set. Color(left. Pixel. get. Color()); } } }
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
- Slides: 14