100 likes | 345 Views
Repetition. Pages 61-68. 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
E N D
Repetition Pages 61-68
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
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); }
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
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); }
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
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.
Nested Loops strokeWeight(4); for (int y = 10; y <100; y = y+10) // dimension y point(10,y); for (intx = 10; x<100; x = x+10) // dimensión x point(x,10); //nestedfor .. Twodimensions for (int y = 10; y <100; y = y+10) // dimension y for(int x = 10; x <100; x = x+10) // dimensión x point(x,y);
More Examples noStroke(); for(int y = 10; y <100; y = y+10) // dimensiony for(int x = 10; x <100; x = x+10) // dimensión x { fill((x+y)*1.4); rect(x,y,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.