Presentation is loading. Please wait.

Presentation is loading. Please wait.

Pages Section: Control2: Repetition

Similar presentations


Presentation on theme: "Pages Section: Control2: Repetition"— Presentation transcript:

1 Pages 61-68 Section: Control2: Repetition

2 Introduction Three major types of language constructs or statements:
Sequential statement Decision/selection statement Repetition/iteration statement Iterative statements Are used to compact length lines of repetitive code Expresses concisely a repetitive operation We will learn “for” statement of Processing

3 Examples line(20, 20, 20, 180); line(30, 20, 30, 180); line(40, 20, 40, 180); line(50, 20, 50, 180); line(60, 20, 60, 180); line(70, 20, 70, 180); line(80, 20, 80, 180); line(90, 20, 90, 180); line(100, 20, 100, 180); for (i = 20; i< 120; i= i+ 10) { line(i,20,i,180); }

4 For syntax and semantics
for (init; test; update) { statements } The init statement is executed The test is evaluated to true or false If the test is true, execute the statement within the block indicated by { } If the test is false skip to the statement after the “for” statement

5 More examples for (int x = -16; x <100; x = x + 10) { line(x,0,x+15, 50); } strokeWeight(4); for (int x = -8; x , 100; x = x + 10) { line (x,50,x+15, 100); }

6 More Examples noFill(); for (int d = 150; d > 0; d = d -10) { ellipse (50, 50, d, d); } Try some of the examples in page 64

7 Nested Iteration A “for” statement produces repetition in one dimension. Nesting a “for” within a “for” generates “looping” in 2 dimensions. Lets look at some examples.

8 Nested Loops strokeWeight(4); for (int y = 10; y <100; y = y+10) // dimension y point(10,y); for (int x = 10; x <100; x = x+10) // dimensión x point(x,10); //nested for .. Two dimensions point(x,y);

9 More Examples noStroke(); for (int y = 10; y <100; y = y+10) // dimension y for (int x = 10; x <100; x = x+10) // dimensión x { fill((x+y)*1.4); rect(x,y,10,10); }

10 Summary We studied the syntax, semantics and purpose of “for” statement We also looked at many examples Make sure you format the statements for readability, esp. now that we are adding complex statements to your code.


Download ppt "Pages Section: Control2: Repetition"

Similar presentations


Ads by Google