Presentation is loading. Please wait.

Presentation is loading. Please wait.

Repetition 8/9/2009. Learning to Program -- Suggestions Spend a lot of time fiddling around with code –Programming is something you have to learn by doing.

Similar presentations


Presentation on theme: "Repetition 8/9/2009. Learning to Program -- Suggestions Spend a lot of time fiddling around with code –Programming is something you have to learn by doing."— Presentation transcript:

1 Repetition 8/9/2009

2 Learning to Program -- Suggestions Spend a lot of time fiddling around with code –Programming is something you have to learn by doing. Get help from other people –If you know how to program, help others Ask questions

3 So far… We've seen how to write commands in Processing –We've learned that commands need to end with a ; –We've learned how to call methods with specific arguments –We've learned how to declare variables and assign values to them –We've learned how to print values to the text output area –We've learned about the primitive variable types –We've learned how to control flow with conditional if- else statements By default, the sequence of commands we write in Processing will execute once and then the program will terminate

4 Motivation size(300,300); background(0); stroke(255); point( (width/11)*1, height/2); point( (width/11)*2, height/2); point( (width/11)*3, height/2); point( (width/11)*4, height/2); point( (width/11)*5, height/2); point( (width/11)*6, height/2); point( (width/11)*7, height/2); point( (width/11)*8, height/2); point( (width/11)*9, height/2); point( (width/11)*10, height/2);

5 A Better Way size(300,300); background(0); stroke(255); int i = 1; while(i<11) { point( (width/11)*i, height/2 ); i++; }

6 while Loops while( ) { } Executes the code within the code block (or the curly brackets) while the boolean expression is true

7 ++ The Increment Operator -- The Decrement Operator A program often needs to increase (or decrease) the value of an integer variable by one, especially in loops. So there is a quick way to write this: x++ is the same as writing x = x + 1 x-- is the same as writing x = x - 1

8 Other Operators Operators in Java/Processing also include shorthand ways to write simple addition, subtraction, multiplication and division statements: x += 2 is the same as writing x = x + 2 x *= 3 is the same as writing x = x * 3

9 Checkers With Loops size(500,500); int xPos=0, yPos=0; int xInc=(width/2)+1, yInc=(height/2)+1; int xSize=width/2, ySize=height/2; int count=0; while (count <4) { if(count == 1) { xPos=xPos+xInc; } else if(count == 2) { yPos=yPos+yInc; } else if(count == 3) { xPos=xPos-xInc; } if(xPos%2 == yPos%2) { fill(0); } else if (xPos%2 != yPos%2) { fill(255); } rect(xPos, yPos, xSize, ySize); count++; }

10 for loops for ( ; ; ) { } First executes the initialization statement Then tests the boolean expression – if it's true, executes the code inside the curly brackets once Then repeats the following: execute final statement, test boolean expression, execute code if true This structure is often used to execute a block of code a specific number of times as follows: Init statement -> start counter, e.g. int counter = 0; Boolean exp -> looping condition, e.g. counter < 10; Final statement -> increment counter, e.g. counter++; (same as counter=counter+1;)

11 Example int i, spacing; spacing=width/5; for (i=1; i<5; i++) { ellipse(spacing*i,spacing*i,spacing,spacing); }

12 An Example from the Text size(500,500); for(int d=width; d>0; d-=10) { ellipse(width/2, height/2, d, d); }

13 Converting a for Loop to a while Loop Seeing how for loops can be converted to while loops helps you understand for loops for( ; ; ) { } is the same as while( ) { }

14 Example int max =500; size(max,max); colorMode(RGB, max); for(int i=0; i<=max; i++) { stroke(i,0,0); line(i, 0, i, height); } int max =500; size(max,max); colorMode(RGB, max); int i=0; while(i<=max) { stroke(i,0,0); line(i, 0, i, height); i++; }

15 Nested Loops noStroke(); colorMode(RGB, 100); for(int i=0; i<100; i++) { for(int j=0; j<100; j++) { stroke(i, j, 0); point(i, j); }

16 Usage Use a for loop if you know how many times you want to repeat. Use a while loop if you don’t know how many times you want the loop to execute. The while loop is considered a general purpose loop construct, but remember the test or boolean expression is evaluated outside the loop, so the loop body may not execute.

17 In-class Lab Initialize variables x and y to 0. Using a while statement, create a program that writes a point out at (x,y), and incrementing x and y at each repetition until x==width or y==height. Create a regular pattern with 5 lines using a for statement. Create an image (where you change every pixel in the window) using a nested for loop.


Download ppt "Repetition 8/9/2009. Learning to Program -- Suggestions Spend a lot of time fiddling around with code –Programming is something you have to learn by doing."

Similar presentations


Ads by Google