Computer Science 1026 Topic 6a.docx

2 Pages
Unlock Document

Western University
Computer Science
Computer Science 1026A/B
Brian Langen

Topic 6 – Loops Modifying Pictures  Recall that we manipulate pictures by manipulating the pixels in the picture  We can change the color value of: o Just 1 pixel o Some of the pixels in a picture  A whole column of pixels  Pixels in some sub-area of the pixcture. o Every pixel in the pixel  One way to change a picture is to reduce the amount of red in it o Decrease it by half o Increase it by 25% o Ex. Changing the red in „caterpillar.jpg‟  Get the current pixel  Get the red value of the current pixel  Change this value to half its original value  Set the red of the current pixel to the new value Iteration (Looping)  Java has a construct that allows us to repeat a series of statements some number of times: called a loop/iteration  A loop should not go on forever, so we need some way to tell them when we are done with the repetition, some „test‟ to see if the looping should stop  IF you want to do something x times, you often need a counter o A counter may start with 0 or 1, depending on the situation o You add 1 each time you finish whatever it is you are repeating o When the counter reaches the appropriate number, you stop the loop  Ex. While whatever is inside the „while loop‟ is true, repeat what is inside the loop. If what is inside the loop becomes false, it will end the loop and move on. o int count = 1; o While (count <= 5) { o System.out.println(“This is a test”) o Count = count + 1; }  The basic syntax for a while loop is: o While (test) { body of loop } o Test = a condition that is true or false o Body of the loop = statements to be executed while the condition is true  When the condition becomes false, the execution continues with the statement after when while loop, we say it „falls through‟ after the while loop  A while loop is a „pretested loop‟  Something must change within the body of the loop that will cause the condition to become false, otherwise an infinite loop will be created  We will now develop the code for a method to decrease red in a picture
More Less

Related notes for Computer Science 1026A/B

Log In


Don't have an account?

Join OneClass

Access over 10 million pages of study
documents for 1.3 million courses.

Sign up

Join to view


By registering, I agree to the Terms and Privacy Policies
Already have an account?
Just a few more details

So we can recommend you notes for your school.

Reset Password

Please enter below the email address you registered with and we will send you a link to reset your password.

Add your courses

Get notes from the top students in your class.