{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

cs026_6_Part3

# cs026_6_Part3 - Topic 5 Part 3 Modifying Pictures Using...

This preview shows pages 1–7. Sign up to view the full content.

1 Topic 5 Part 3 Modifying Pictures Using Loops (For Loops) Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B. Ericson, and instructor materials prepared by B. Ericson.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
2 Learning Goals To understand at a conceptual and practical level: How to manipulate digital images using various methods What is a for loop
3 Another Kind of Loop: For Loop Programmers like shortcuts Especially those that reduce errors And mean less typing We have been using a while loop with an index We had to declare the index variable and initialize it before the loop If you forget this, there will be a compiler error We had to increment the index in the loop If you forget this, it will be an infinite loop The shortcut for this is a for loop

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
4 Example: Add the Numbers from 1 to 100 int total = 0; for (int counter = 1; counter <=100; counter++) { total = total + counter; } System.out.println(“Sum of 1 to 100 is ” + total);
5 For Loop Syntax for (start; check; step) { body of loop } Start (initialization area) Declare loop variables and initialize them Check (continuation test) If true do body of loop If false jump to next statement after the loop Step (change area) Change the loop variables Increment or decrement them

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
6 For Loops: How Do They Work? See our example to add up 1 to 100: for (int counter = 1; counter <=100; counter++) Step 1: the loop variable counter is declared and initialized This is only done once; it is not repeated each time through the loop Step 2: the test is performed If the condition counter <=100 is true, go to Step 3 If the condition is false, go to the statement after the body of the loop Step 3: the body of the loop is executed
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern