- Repeated Elements
- Source code repetition: advantages
- Source code repetition: disadvantages
- Preparing for iteration
- Infinite Loops
- The for statement
- Controlling loop count
- Unrolling the loop
- Effects
- Counting
- Drawing one shape differently
- Modifying x and y
Repeated Elements
Draw four circles by writing four calls to circle
.
Add two more circles by adding two more lines of code.
Source code repetition: advantages
There is a simple correspondence between lines of code, and generated graphics.
It is easy to change just one repeated element.
Source code repetition: disadvantages
Changing many elements requires editing many lines of code.
Drawing a large number of elements would require a lot of code.
Preparing for iteration
We will prepare the code for iteration the same way that a different sequence prepared it to use functions: by making the repeated sections of source code look more similar to each other.
Let's pare back to four circles, and demonstrate a different way to repeat multiples.
Introduce a variable, so that the calls to circle
all look the same.
Make the lines that set y
to a new value look the same.
y += 50
is a shortcut for y = y + 50
.
Add a final y += 50
. This doesn't change the behavior because y
isn't used again. It makes all the stanzas the same, though.
We can use {
and }
to group a block of code. This doesn't change its meaning.
We only want to keep drawing circles until we reach the bottom circle, that has a y
value of 200. The if
statements make this explicit. Since y <= 200
all four times, this code has the same behavior as the previous version. (We would still have to add more code if we wanted the sketch to be able to draw more than four circles.)
if
means to do a thing zero or one times. while
means keep doing it: do it zero or more times. Replace the multiple if
statements by a single while
.
Now it's easy to change the spacing ( change y += 50
to e.g. y += 80
):
Or the termination condition (change y <= 200
to e.g. y <= 400
).
These would be bigger changes, if each line of circle were drawn by a different line of code.
Infinite Loops
It's also easy to write an infinite loop! This while
statement never finishes, so draw
never returns.
This is also an infinite loop, because when a p5 sketch starts, mouseX and mouseY both have the value 0 the first time that draw
is called
mouseX
and mouseY
are updated between calls to draw
, so moving the mouse while the loop is running doesn't break out of it.
A solution. max(mouseY, 1)
has the value 1
when mouseY
is less than 1, and the value mouseY
when mouseY ≧ 1
.
The for
statement
This pattern of using a variable that changes value each time through the loop, and whose value is used in the termination test, is so common that there's a special statement, for
, that indicates that a loop is used this way.
A for
statement has the form for (initialization; test; increment) { body }
. This collects the code that has to do with controlling the loop into a single line.
These two functions are equivalent. They each initial y
to 50, increment it by 50 each time, draw circles while y
is less or equal to 200.
We can demonstrate this by writing a single function that uses both forms of loop.
Controlling loop count
The code above repeats circles until they've reached a certain y position.
We can draw an exact number of circles, instead, by using a variable that counts the number of circles that we've drawn.
Let's modify the code from above to count circles. We do this by adding an iteration variable, i
, that counts the number of circles. The code on the left is the code from above, that draws however many circles it takes to reach the y
value. The code in the middle and the code on the right shows two different ways to count the number of times through the loop body: by counting from 1 to 10, and by counting from 0 to 9.
Using while
:
Using for
:
Notes:
- Usually a
for
loop is used for this, rather thanwhile.
- Usually
i
is used as the iteration variable. If one loop is inside another one,i
is used for the outer loop andj
for the inner one. - It is common to count from 0 and using
<
, than to count from 1 and use<=
or==
. When we cover arrays, we will see why. - Just as
i += 1
is an abbreviation fori = i + 1
,i++
is an abbreviation fori += 1
. Thefor
line in the final sketch above could be writtenfor (let i = 0; i < 10; i++)
.
Unrolling the loop
For review: all of the following sketches are equivalent.
Since i
is not used in the unrolled version, and since we know the value that y
has at each line, these are also equivalent to:
Effects
Counting
This code, similar to the code above increments y
by a constant amount until it reaches the bottom of the screen:
We can instead increase y
by 10% each time through the loop. (y *= 1.1
is an abbreviation of y = y * 1.1
, which increases the value of y
by 10%.)
Drawing one shape differently
You may recall that one of the disadvantages of using iteration, instead of one line of code for each element, is that it is more difficult to modify a single element in a repetition.
You can still do this. Here are three different ways to draw one element differently.
Modifying x and y
We can increase by x
and y
.
Since y
is used to control the loop termination, it makes sense to use it as the iteration variable (above), although we could also do either of these:
©2020–2022 by Oliver Steele.