Repetition PAGES 61 68 SECTION CONTROL 2 REPETITION

  • Slides: 10
Download presentation
Repetition PAGES 61 -68 SECTION: CONTROL 2: REPETITION

Repetition PAGES 61 -68 SECTION: CONTROL 2: REPETITION

Introduction �Three major types of language constructs or statements: Sequential statement Decision/selection statement Repetition/iteration

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, 180); line(30, 20, 30, 180); line(40, 20, 40, 180); line(50, 20,

Examples line(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 } 1. The init

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

More examples for (int x = -16; x <100; x = x + 10)

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

More Examples no. Fill(); for (int d = 150; d > 0; d =

More Examples no. Fill(); for (int d = 150; d > 0; d = d -10) { ellipse (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

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 stroke. Weight(4); for (int y = 10; y <100; y = y+10)

Nested Loops stroke. Weight(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 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 no. Stroke(); for (int y = 10; y <100; y = y+10)

More Examples no. Stroke(); 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); }

Summary �We studied the syntax, semantics and purpose of “for” statement �We also looked

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.