Our First Loop
So far you've been writing each addLine and turn call one at a time. When a shape has many sides, that gets repetitive fast. Swift's for loop lets you run the same block of code a set number of times.
Here's the basic syntax:
for i in 1...4 { // this block runs 4 times (i = 1, 2, 3, 4) pen.addLine(distance: 100) pen.turn(degrees: 90) }
The 1...4 is a closed range — it includes both 1 and 4. You can use any range you like. If you don't need the loop variable i, Swift convention is to replace it with _:
for _ in 1...4 { pen.addLine(distance: 100) pen.turn(degrees: 90) }
This draws a square in just 4 lines instead of 8! Let's put loops to work on real shapes.
| Standard | Connection |
|---|---|
| Expressions & Equations | Loops introduce the idea of repeated operations — a foundation for understanding iterative processes and sequences. |
Triangle
An equilateral triangle has 3 equal sides and 3 equal angles. Each exterior angle is 120°. Use a for loop to draw one.
for _ in 1...3 { pen.addLine(distance: 150) pen.turn(degrees: 120) }
| Standard | Connection |
|---|---|
| Geometry — Triangles | Equilateral triangles have three 60° interior angles; the exterior angle of 120° is used as the turn angle. Interior + exterior = 180°. |
Squares
Draw a row of three squares side by side using loops. Each square should have sides of length 80. Use move to reposition the pen without drawing between squares.
Hint: after drawing each square, you'll need to lift the pen and move it to the starting corner of the next square.
for _ in 1...3 { // Draw one square for _ in 1...4 { pen.addLine(distance: 80) pen.turn(degrees: 90) } // Move to start of next square pen.move(distance: 80) }
| Standard | Connection |
|---|---|
| Geometry — Quadrilaterals | A square has four equal sides and four 90° angles. The perimeter of each square is 4 × 80 = 320 units. |
Dashes
Draw a dashed line by alternating between drawing a short segment and moving (without drawing) a short gap. Repeat 10 times to create the dashed effect.
for _ in 1...10 { pen.addLine(distance: 20) // draw dash pen.move(distance: 10) // skip gap }
| Standard | Connection |
|---|---|
| Patterns & Sequences | Dashed lines are a repeating pattern — a visual sequence where each unit consists of a drawn segment and a gap. |
Regular Polygons
A regular polygon with n sides has an exterior angle of 360 ÷ n degrees. Use a variable for the number of sides and write one loop that draws any regular polygon. Try it with a pentagon (5 sides), hexagon (6), and octagon (8).
let sides = 6 // change this to try other polygons let angle = 360 / sides for _ in 1...sides { pen.addLine(distance: 100) pen.turn(degrees: Double(angle)) }
| Standard | Connection |
|---|---|
| Geometry — Polygons | The exterior angle of a regular n-gon is 360°/n. Interior angle = 180° − exterior angle. All exterior angles of any convex polygon sum to 360°. |
Stars
A 5-pointed star is drawn by turning 144° at each point (skipping one vertex each time). Use a loop with 5 iterations to draw a star. Then try a 6-pointed star with a turn of 120°.
Hint: for a 5-pointed star you take 5 steps and turn 144° each time (5 × 144° = 720° = 2 full rotations).
for _ in 1...5 { pen.addLine(distance: 150) pen.turn(degrees: 144) }
for _ in 1...6 { pen.addLine(distance: 150) pen.turn(degrees: 120) }
| Standard | Connection |
|---|---|
| Geometry — Angles | The turn angle for a {n/k} star polygon is (k × 360°)/n. For a {5/2} pentagram: (2 × 360°)/5 = 144°. |
Something Different
What happens when the turn angle doesn't divide evenly into 360°? Experiment with non-standard angles like 97° or 137° and run many iterations. You'll discover some surprisingly beautiful patterns.
Try at least three different angles with 50+ iterations and describe what you observe. Which angle created your favourite pattern?
for _ in 1...100 { pen.addLine(distance: 80) pen.turn(degrees: 137.5) }
| Standard | Connection |
|---|---|
| Number & Ratio | The golden angle (~137.508°) is derived from the golden ratio φ ≈ 1.618. It appears in phyllotaxis — the spiral packing of seeds in sunflowers and pine cones. |
Loop in a Loop
You can place a loop inside another loop — this is called a nested loop. The inner loop completes all its iterations for each single iteration of the outer loop.
Use a nested loop to draw a ring of squares: an outer loop runs 8 times, and each iteration draws one complete square (inner loop), then turns the pen by 45° to position it for the next square.
for _ in 1...8 { // Draw one square for _ in 1...4 { pen.addLine(distance: 60) pen.turn(degrees: 90) } // Rotate to next position in the ring pen.turn(degrees: 45) }
| Standard | Connection |
|---|---|
| Geometry — Rotational Symmetry | Dividing 360° by 8 gives 45° — the rotation needed to evenly space 8 squares around a full circle. The result has 8-fold rotational symmetry. |
Spirals
Create a spiral by using the loop variable i to make each line slightly longer than the last. This is where having a named loop variable pays off!
In the loop below, i starts at 1 and increases to 50. Use i directly as the line distance so each segment is longer:
for i in 1...50 { pen.addLine(distance: /* use i here */) pen.turn(degrees: 91) // try different angles! }
Try adjusting the turn angle — small changes produce very different spirals. What angle gives the tightest coil?
for i in 1...50 { pen.addLine(distance: Double(i) * 3) pen.turn(degrees: 91) }
for i in 1...200 { pen.addLine(distance: Double(i) * 0.5) pen.turn(degrees: 15) }
| Standard | Connection |
|---|---|
| Patterns & Linear Functions | The line distance grows linearly with i (arithmetic sequence). Plotting distance vs. iteration number gives a straight line — a linear relationship. |