1 / 10

Repetition

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

woods
Télécharger la présentation

Repetition

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Repetition Pages 61-68

  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 (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);

  9. 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); }

  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.

More Related