CSE 113 Week 5 February 11 - 15, 2008

advertisement
CSE 113
Week 5
February 11 - 15, 2008
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
Highlights
 What if we wanted to change all of
the pixels in a picture to black?
 We need to access all of the pixels of
the picture one at a time and then
change their color to black.
Highlights
 We can’t do this simply by inputting
each coordinate of the pixels and
changing them, because it only works
for certain images, not all images.
 What we want to do is repeat the
process of getting a pixel and
changing its color.
Highlights
 We need a way to help us repeat.
 Repetition is a key element of
computation.
 One way to have a program repeat is
to add a loop to our program.
 The loop we will look at first is a forloop
Highlights
 The for-loop is a counting loop, so
much of the syntax needed helps us
to set up the counting of how many
times the loop should repeat.
 For-loop syntax
for(initialization; test; increment) {
//loop body
}
Highlights
 initialization
 Create a variable that is our loop counter
and give it a starting value
 test
 Test to tell us when to stop the counting
 increment
 Increments the counter after each time
the loop body is executed
Highlights
 We can start a count with the first
pixel in a row/column and continue
counting until the end of the row or
column
 Remember that we can tell the width
and height of a picture by calling the
appropriate methods on it
Highlights
 In order to go through all the pixels
(width x height), we will need a
system of two loops, one that loops
through the columns, and the other
that loops through the rows.
 Putting them together allows us to go
through all the rows and columns in
the picture.
Download