cs026_6_Part3

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

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 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. 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 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 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 • Step 4: the loop variable counter is incremented • Go back to the test (Step 2) 7...
View Full Document

This note was uploaded on 12/20/2009 for the course COMP SCI 026 taught by Professor Unknown during the Spring '07 term at UWO.

Page1 / 22

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

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online