![Page 1: 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](https://reader036.vdocuments.us/reader036/viewer/2022083009/5697c00b1a28abf838cc7fdd/html5/thumbnails/1.jpg)
Repetition8/9/2009
![Page 2: 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](https://reader036.vdocuments.us/reader036/viewer/2022083009/5697c00b1a28abf838cc7fdd/html5/thumbnails/2.jpg)
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
![Page 3: 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](https://reader036.vdocuments.us/reader036/viewer/2022083009/5697c00b1a28abf838cc7fdd/html5/thumbnails/3.jpg)
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
![Page 4: 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](https://reader036.vdocuments.us/reader036/viewer/2022083009/5697c00b1a28abf838cc7fdd/html5/thumbnails/4.jpg)
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);
![Page 5: 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](https://reader036.vdocuments.us/reader036/viewer/2022083009/5697c00b1a28abf838cc7fdd/html5/thumbnails/5.jpg)
A Better Way
size(300,300);background(0);stroke(255);int i = 1;while(i<11) {point( (width/11)*i, height/2 );i++;
}
![Page 6: 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](https://reader036.vdocuments.us/reader036/viewer/2022083009/5697c00b1a28abf838cc7fdd/html5/thumbnails/6.jpg)
while Loops
while(<boolean exp>) {
<code to execute multiple times>
}
• Executes the code within the code block (or the curly brackets) while the boolean expression is true
![Page 7: 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](https://reader036.vdocuments.us/reader036/viewer/2022083009/5697c00b1a28abf838cc7fdd/html5/thumbnails/7.jpg)
++ 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
![Page 8: 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](https://reader036.vdocuments.us/reader036/viewer/2022083009/5697c00b1a28abf838cc7fdd/html5/thumbnails/8.jpg)
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
![Page 9: 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](https://reader036.vdocuments.us/reader036/viewer/2022083009/5697c00b1a28abf838cc7fdd/html5/thumbnails/9.jpg)
Checkers With Loopssize(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++;}
![Page 10: 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](https://reader036.vdocuments.us/reader036/viewer/2022083009/5697c00b1a28abf838cc7fdd/html5/thumbnails/10.jpg)
for loopsfor (<init statement>; <boolean exp>; <final statement>) { <code to execute in loop>}
• 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;)
![Page 11: 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](https://reader036.vdocuments.us/reader036/viewer/2022083009/5697c00b1a28abf838cc7fdd/html5/thumbnails/11.jpg)
Example
int i, spacing;
spacing=width/5;
for (i=1; i<5; i++) {
ellipse(spacing*i,spacing*i,spacing,spacing);
}
![Page 12: 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](https://reader036.vdocuments.us/reader036/viewer/2022083009/5697c00b1a28abf838cc7fdd/html5/thumbnails/12.jpg)
An Example from the Text
size(500,500);
for(int d=width; d>0; d-=10) {
ellipse(width/2, height/2, d, d);
}
![Page 13: 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](https://reader036.vdocuments.us/reader036/viewer/2022083009/5697c00b1a28abf838cc7fdd/html5/thumbnails/13.jpg)
Converting a for Loop to a while Loop
• Seeing how for loops can be converted to while loops helps you understand for loops
for(<init statement>; <boolean exp>; <final statement>) { <code>}
is the same as
<init statement>while(<boolean exp>) { <code> <final statement>
}
![Page 14: 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](https://reader036.vdocuments.us/reader036/viewer/2022083009/5697c00b1a28abf838cc7fdd/html5/thumbnails/14.jpg)
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++;}
![Page 15: 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](https://reader036.vdocuments.us/reader036/viewer/2022083009/5697c00b1a28abf838cc7fdd/html5/thumbnails/15.jpg)
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); } }
![Page 16: 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](https://reader036.vdocuments.us/reader036/viewer/2022083009/5697c00b1a28abf838cc7fdd/html5/thumbnails/16.jpg)
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.
![Page 17: 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](https://reader036.vdocuments.us/reader036/viewer/2022083009/5697c00b1a28abf838cc7fdd/html5/thumbnails/17.jpg)
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.