repetition 8/9/2009. learning to program -- suggestions spend a lot of time fiddling around with...

17
Repetition 8/9/2009

Upload: noel-green

Post on 18-Jan-2016

212 views

Category:

Documents


0 download

TRANSCRIPT

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

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

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

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

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

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

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

++ 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

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

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

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

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

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

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

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

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

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

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.