Download presentation
Presentation is loading. Please wait.
Published byAugustus Burns Modified over 8 years ago
1
Loops
2
About the Midterm Exam.. Exam on March 12 Monday (tentatively) Review on March 5
3
3 What is Iteration? Something that repeats Each ‘iteration’ may do something different Usually has a defined condition to stop repeating You have already ‘used’ loops Processing calls draw() over and over until you end your program.
4
4 Why use Iteration? Without Iteration: stroke(0); line( 50,60, 50,80); line( 60,60, 60,80); line( 70,60, 70,80); line( 80,60, 80,80); line( 90,60, 90,80); line(100,60,100,80); line(110,60,110,80); line(120,60,120,80); line(130,60,130,80); line(140,60,140,80); line(150,60,150,80);
5
5 Why use Iteration? Without Iteration: stroke(0); line( 50,60, 50,80); line( 60,60, 60,80); line( 70,60, 70,80); line( 80,60, 80,80); line( 90,60, 90,80); line(100,60,100,80); line(110,60,110,80); line(120,60,120,80); line(130,60,130,80); line(140,60,140,80); line(150,60,150,80); Study what changes x’s increase each time What is the pattern? Add 10 each time When does it stop? Last line is at x = 150
6
6 The only Loop You Really Need: while Use a Boolean Expression to test if we are done Just like in if-else There are there other types loops available: do-while for Action(s) Condition True False
7
7 Using Iteration Use the same ‘setup’ Put the repetitive code inside the loop Put your exit condition in the ‘test’ // Loop Version int x = 50; int spacing = 10; int ending = 150; while(x <= ending) { line(x,60,x,80); x = x + spacing; } stroke(0); line( 50,60, 50,80); line( 60,60, 60,80); line( 70,60, 70,80); line( 80,60, 80,80); line( 90,60, 90,80); line(100,60,100,80); line(110,60,110,80); line(120,60,120,80); line(130,60,130,80); line(140,60,140,80); line(150,60,150,80);
8
Let’s see if we can plan and write a loop… 8 Plan the three parts: What is the initial condition for the loop? int y = 10; When should the loop stop? while (y < height){ // loop } What is the loop operation? rect(50,y,100,10); y = y + 20;
9
And the code for the loop ‘falls out’… 9 void setup() { size(200,200); background(255); fill(125); } void draw() { int y = 10; while(y < height) { rect(50,y,100,10); y = y + 20; }
10
Exercise: 10
11
11 Your next loop: for Use a for loop when: 1) Your ‘test’ variable is a number 2) You know when to stop before you start for( Setup variables ; Test Condition ; Change test variable ){ for( int x = 1; x < 10 ; x = x + 1) { // Action(s) go here } Action(s) Change test variable Test Condition True False Setup
12
12 for loop Syntax and Example Start at 0 and count up to 9 for (int i = 0; i < 10; i = i + 1) Start at 0 and count up to 100 by 10 for (int i = 0; i < 101; i = i + 1) Start at 100 and count down to 0 by 5 for (int i = 100; i >= 0; i = i – 5)
13
Increment/Decrement Operators x++; x = x + 1; x--; x = x - 1; x+ = 2; x = x + 2; x* = 3; x = x * 3;
14
Exercise: While to for loop 14
15
Local vs. Global Variables Global variables are declared at the top of the program, outside of setup() and draw(). Local variables are declared within a block of code. setup(), draw(), mousePressed(), keyPressed()… if statements, while and for loops For example, for (int i = 0; i < 100; i++)
16
Example 6-4 // Global " count " int count = 0; void setup() { size(200,200); } void draw() { count = count + 1; background(count); } ________ 16 // Local " count " void setup() { size(200,200); } void draw() { int count = 0; count = count + 1; background(count); } ________ // Local " count " void setup() { size(200,200); } void draw() { int count = 0; count = count + 1; background(count); } ________ Global vs. Local variables in loops Which will these produce after 100 frames? A, B or C?
17
Loop Inside the Main Loop Remember that draw() is called in a ‘loop’ This is often called the ‘main’ loop The last thing that draw() does is... Paints the screen How could you make a set of parallel horizontal lines on the screen? 17
18
Exercise Render one line at a time 18
19
int endY; void setup() { size(200,200); frameRate(5); endY = ________; } void draw() { background(255); for (int y = ________; ________; ________) { stroke(0); line(0,y,width,y); } ________; }
20
How to measure distance between points Take one axis (X) for example: float distance = abs(mouseX – i); abs() function returns a positive number Absolute value Exercise: Write the program using a for loop Rewrite the program using a while loop 20
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.