Repeating Things 🔁¶
Let the computer do the boring part: again, and again, and again
Why Repeat? 🤔¶
Say you want a row of five balloons. You could write five lines that are almost the same:
Canvas paper having width 400, length 200
draw circle at 60, 100 having radius 25, fill "red"
draw circle at 120, 100 having radius 25, fill "red"
draw circle at 180, 100 having radius 25, fill "red"
draw circle at 240, 100 having radius 25, fill "red"
draw circle at 300, 100 having radius 25, fill "red"
Or you can tell the computer to repeat one line five times:
Canvas paper having width 400, length 200
repeat 5 times {
draw circle at 60 * count, 100 having radius 25, fill "red"
}
Everything inside the curly brackets { } happens five times. This is called a loop.
Counting with count 🔢¶
Inside a loop, count tells you which time round it is: 1 the first time, then 2, then 3, and so on.
That's how each balloon above lands in a different place. 60 * count is 60 the first time, 120 the second time, 180 the third time...
You can use count anywhere a number goes, so things can grow too:
Canvas paper having width 500, length 250
repeat 6 times {
draw circle at 70 * count, 150 having radius 5 * count, fill "skyblue", stroke "steelblue"
}
Or shrink. These rings get smaller each time round:
Canvas paper having width 350, length 300
repeat 5 times {
draw circle at 200, 150 having radius 120 - 20 * count, stroke "purple", thickness count
}
Polygons can grow more sides each time round. This one goes from a triangle to an octagon:
Canvas paper having width 650, length 200
repeat 6 times with n {
draw polygon at 90 * n, 120 having sides n + 2, size 80, fill "plum", stroke "purple"
}
Giving the Counter a Name 🏷️¶
You can choose your own name for the counter with with:
Canvas paper having width 350, length 250
repeat 5 times with step {
draw square at 50 * step, 220 - 40 * step having size 40, fill "orange", stroke "brown"
}
A good name makes the loop easier to read, like step for a staircase.
Loops Inside Loops 🔁🔁¶
Put a loop inside another loop, and you get rows and columns. Each loop needs its own counter name, so you know which is which:
Canvas paper having width 400, length 300
repeat 4 times with row {
repeat 6 times with col {
draw circle at 55 * col, 55 * row having radius 3 + 3 * row, fill "teal"
}
}
The outer loop goes round 4 times, and each time, the inner loop goes round 6 times. That's 24 circles from 3 lines of code!
If both loops used count, the inner count would hide the outer one, and you couldn't tell rows from columns. So VeeLang asks you to name them:
Canvas paper having width 400, length 250
repeat 4 times {
repeat 6 times {
draw circle at 55 * count, 55 * count
}
}
Names Inside a Loop 🔂¶
A shape made inside a loop can have a name, but the name only lasts for one time round. That's handy for drawing something on top of it:
Canvas paper having width 400, length 250
repeat 4 times with n {
draw circle balloon at 80 * n, 100 having radius 30, fill "pink"
draw line from balloon.center.x, 130 to balloon.center.x, 220 having color "gray"
}
Each time round, balloon is the balloon from that time. After the loop, the name is gone, because there were four balloons, and VeeLang wouldn't know which one you meant.
Repeating Parts of a Shape 🧱¶
Loops work inside a shape's { } too. Then everything the loop makes is a part of that shape, measured from it, and moves with it:
Canvas paper having width 400, length 300
draw rectangle fence at 40, 200 having width 320, length 12, fill "peru" {
repeat 7 times with n {
rectangle at 45 * n - 30, -50 having width 20, length 90, fill "burlywood"
}
}
Change the fence's at and the whole fence moves, pickets and all.
Moving Step by Step 👣¶
A loop can change a shape a little bit each time round:
Canvas paper having width 300, length 250
circle ball at 50, 50 having radius 20, fill "red"
repeat 5 times {
ball.x = ball.x + 40
ball.y = ball.y + 30
}
draw ball
The ball moves 40 across and 30 down, five times, and ends up at 250, 200.
Let's Build a Night City 🌃¶
Canvas city having width 400, length 300, fill "midnightblue"
repeat 5 times with b {
draw rectangle building at 75 * b - 60, 300 - 40 * b having width 60, length 40 * b, fill "dimgray" {
repeat b times with floor {
square at 10, 40 * floor - 30 having size 15, fill "gold"
square at 35, 40 * floor - 30 having size 15, fill "gold"
}
}
}
Each building is taller than the last, and the number of floors with lit windows comes from the building's own counter, b.
Challenge Time! 🏆¶
- 🌟 Challenge 1: Draw a row of 10 trees with a loop.
- 🌟 Challenge 2: Draw a chessboard row of 8 squares. Hint: you can't change the color yet, so try just the outlines!
- 🌟 Challenge 3: Use loops inside loops to draw a wall of bricks.
- 🌟 Challenge 4: Make a staircase that goes up to the right.
How many times?
A loop can repeat up to 1000 times, and all the loops in one drawing up to 10000 times together. That's plenty for drawings, and it stops a typo like repeat 50000 times from freezing your computer.
What's Next? 🚀¶
You can now repeat things with repeat, use count or your own counter name, and put loops inside loops and inside shapes.
Next up, the last big idea: inventing your own shapes with recipes! 🍪