{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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

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

View Full Document Right Arrow Icon
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.
Image of page 1

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

View Full Document Right Arrow Icon
2 Learning Goals To understand at a conceptual and practical level: How to manipulate digital images using various methods What is a for loop
Image of page 2
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
Image of page 3

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

View Full Document Right Arrow Icon
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);
Image of page 4
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
Image of page 5

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

View Full Document Right Arrow Icon
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
Image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    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.

    Student Picture

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

  • Left Quote Icon

    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.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    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.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern