Loops Assessment
- Friday, October 4th, 12-4pm, Room 426
- Monday, October 7th, 12-2:30pm, Room 408
- Take your computer and pen
- Part on paper, part on computer
Loops review
- Loops are counters. They count how many times you should run code. You can also use the count as a variable to do something.
- You can count columns.
- You can count pixels.
- You can count indexes.
- You can count …
- Loop anatomy and syntax: 3 parameters
- Where to start: Initialization
let i = 0
- When to exit: Boolean test
i < width
- How to get from start to finish: Incrementation operation
i++
, i+=10
for (let i = 0; i < width; i++) {
}
- When writing code, as soon as you see a pattern in numbers, you can re-write it as a loop.
- for() loops versus draw() loop. Code in for() loops all happen in 1 frame of animation. The draw() loop happens over time.
Worksheet and Assignment review
- Checkerboard (counting columns)
let cols = 10;
let rows = 10;
function setup() {
createCanvas(400, 400);
}
function draw() {
background(220);
for (let r = 0; r < 10; r++) {
for (let c = 0; c < 10; c++) {
if (r % 2 == 0 && c % 2 == 0) {
fill(0);
} else if (r % 2 == 1 && c % 2 == 1) {
fill(0);
} else {
fill(255);
}
rect(
(c * width) / cols,
(r * height) / rows,
width / cols,
height / rows
);
}
}
}
https://editor.p5js.org/icm-nun/full/5qzKVtgBp
<aside>
✏️
Test yourself (loops)
</aside>