| CPSC 120 | Principles of Computer Science | Fall 2025 | 
Loops provide a powerful shortcut for repetition — instead of cutting and pasting (with lots of small, tedious, error-prone edits) to get multiple copies of something, a loop allows you to repeat things as often as you want with only a few extra lines of code.
This week's lab focuses on breaking up complex pictures into loops, and working with the different loop patterns discussed in class (repeat-as-long-as loops and counting loops; one thing in the loop body, multiple things in the loop body; a single loop variable, multiple loop variables; a single loop, one loop after another, nested loops). Quilts once again provide a source of inspiration.
Successfully completing this lab means that you are able to:
recognize applications of the loop patterns identified above
decompose a complex pattern into loops
construct sketches using loops, including
work with loops in Processing, including
Hand in a hardcopy (paper) of your worksheet in class.
To hand in your sketches:
Make sure that your name and a short description of the sketch are included in a comment at the beginning of each sketch.
Make sure that you've auto-formatted each sketch.
Copy the entire lab8a, lab8b, lab8c, and lab8d directories from your sketchbook (~/cs120/sketchbook) to your handin directory (found inside /classes/cs120/handin).
It is OK if you copy your files to the handin directory at the very beginning of class.
Presentation meetings for this lab will be the week of Nov 3.
Exercise #4 is the presentation problem. Come to the presentation meeting prepared to discuss your sketch. You may be asked to point out and explain how your code meets the requirements of the problem, explain how portions of your code work, and/or apply skills from the problem to a new situation.
The policies on late work and extensions, academic integrity, and the use of AI for this lab are the same as for lab 2. Review them there.
There are three main patterns of repeated elements:
    
 
    
 
    
A single loop, with one or more loop variables, applies when the answers to "what is repeated?", "what changes?", and "how do things change?" are the same for all repetitions.
 
    
 
 
    Multiple loops one after another applies when elements can be grouped into two or more groups where the answers for the loop questions ("what is repeated?", "what changes?", "how do things change?", "how do things start?", and/or "when do you keep going?" are the same for all repetitions within one group but different for different groups.
Note: the rightmost example includes both multiple loops and nested loops — the red squares and the blue squares are drawn with separate loops, one after another, with nested loops being used to draw each grid of squares.
 
    
 
    
    Nested loops applies when the answer to "what changes?" and/or "how do things change?" involves "sometimes" — elements can be grouped so that "what changes?" and "how do things change?" is the same for the elements within each group and also between groups (though with different answers between groups than within groups).
With a grid, a common application of nested loops, elements can be grouped into rows and/or columns which are then repeated.
Note: the rightmost example includes both multiple loops and nested loops — the red squares and the blue squares are drawn with separate loops, one after another, with nested loops being used to draw each grid of squares.
Review the slides, in-class exercises handouts, and in-class exercise solutions from the last several weeks — and especially the identifying loops and nested loops materials from Monday — for information, templates, and examples.
Do the exercises in order.
Read through all of each exercise before you start on it. In particular, note that the "to do this" steps are what you should actually do to complete the exercise — don't just read the first sentence of the problem, look at the example, and try to write the sketch from there. Follow the steps!
Use loops whenever it is appropriate to do so! You will receive very little credit for producing the right drawing without loops.
Put your name and a description of the sketch in comments at the beginning of each sketch. Also don't forget to Auto Format your code before handing it in.
Be sure to save your sketch frequently (ctrl-S). (Every time you run your sketch is good.) The editor does not auto-save!
In this exercise you'll create a sketch which draws a strip of 16 flying geese as shown below. Recall from lab 7 that each "goose" is a triangle whose long dimension is twice the length of the short dimension; for a horizontally-oriented pattern like the one shown, this means the height of the triangle is twice the width.
The requirements for your sketch:
Name the sketch lab8a.
The drawing window should be sized to exactly fit 16 triangles that are as tall as the window and half as wide as they are tall.
You can choose your own colors, but each group of four triangles should be the same color and the color of successive groups should change smoothly, as shown. (In the example, the red component of the color increases to the right.)
To do this:
Complete the Exercise 1 section of the lab 8 worksheet.
Create a new sketch, add your name and a description of the sketch in comments at the beginning, and save it as lab8a.
Start with the basic sketch structure: open a drawing window and clear the background.
Write the loop(s) to draw the entire pattern.
In this exercise you'll create a sketch which draws a tumbling blocks quilt, as shown.
The requirements for your sketch:
Name the sketch lab8b.
The drawing window should be square.
Design your sketch so it works with any size window.
The window should be completely filled; you do not need to match the example exactly in terms of the number of repetitions of the pattern. It is OK to draw some elements outside the window to avoid gaps, but you should not draw any blocks entirely outside the window.
You can choose your own colors, but should use lighter, medium, and darker colors in the configuration shown.
To do this:
Create a new sketch, add your name and a description of the sketch in comments at the beginning, and save it as lab8b.
Complete the Exercise 2a section of the lab 8 worksheet.
Write the function definition for drawBlock, then call it from draw() to test it. Remove the call once the function is working.
Complete the Exercise 2b section of the lab 8 worksheet.
Write the loop(s) identified on the worksheet to draw the entire pattern.
In this exercise you'll create a sketch which displays a variation of a block star quilt, as shown. (To see the star pattern, focus on the regions with color rather than the black and white shapes.)
The requirements for your sketch:
Name the sketch lab8c.
The drawing window should be square.
Design your sketch so it works with any size window.
You can choose your own colors, but you should vary one color component horizontally and a different one vertically. (In the example, the red decreases to the right and the blue increases going down.) The black and white elements should be black and white as shown.
To do this:
Create a new sketch, add your name and a description of the sketch in comments at the beginning, and save it as lab8c.
Start with the basic sketch structure: open a drawing window and clear the background.
Complete the Exercise 3a section of the lab 8 worksheet.
Draw the background pattern.
Complete the Exercise 3b section of the lab 8 worksheet.
Draw the white squares.
Complete the Exercise 3c section of the lab 8 worksheet.
Draw the black squares.
Complete the Exercise 3d section of the lab 8 worksheet.
Draw the white quads.
In this exercise you'll create a sketch of your own design. What the sketch depicts is up to you (here's a chance to be creative!) but for full credit it must include the following elements:
Name the sketch lab8d.
The scene must be recognizable as something and/or there must be a deliberately-created geometric pattern. Include a comment at the beginning of the sketch describing what it depicts. The intent is that you should deliberately choose positions and colors for the shapes — simply drawing a bunch of shapes at whatever location they happen to end up at is not acceptable. Simplifying things (like making a tree out of a rectangle and a triangle) is fine. It is OK to have multiple scenes and/or geometric patterns — treat each thing like a quilt block and arrange the blocks into a whole quilt.
The scene must be original and created by you — it can be something entirely new for this exercise or you can extend your sketches for #4 in previous labs. You may not, however, copy code from other exercises in this lab, examples or solutions in the textbook or from class, or other sources even if you then make some changes — create your own version (such as a fancier car) from scratch.
Demonstrate different loop patterns. In particular, there must be at least one instance of each of the following:
Note that you don't necessarily need exactly six loops — one loop can satisfy or go towards satisfying more than one of these criteria at a time, and several criteria require more than one loop to be complete. Include a comment with each loop identifying the pattern.
Combine animation with loops. Include the following:
The two examples mentioned are from Monday's "identifying loops" slides
To do this:
Create a new sketch, add your name and a description of the sketch in comments at the beginning, and save it as lab8d.
For each pattern involving repetition, complete one page of the Exercise 4 section of the lab 8 worksheet before writing the code for the loop(s).
Challenge yourself and earn extra credit by going substantially beyond the required elements. (See the assignments and evaluation policy for more details on extra credit.)
For extra credit, add elements to your lab8d sketch. Some possibilities include:
More — a more elaborate sketch, with additional (appropriately used) loops. (It's the loops that earn extra credit, not the "more" part on its own.)
Demonstrate more complex ways in which things change from one repetition to the next. This could include changing things other than position, size, or color (such as orientation — use rotate()) or accelerating/decelerating the change. The "twisted square and variations" and "circular patterns" examples from the extra credit section in lab 8 have some examples of quilt blocks where orientation varies. Another possibility is repetition in space that isn't in a straight line, such as creating a Ferris wheel or water wheel by positioning things around a circle.
Additional instances of combining loops with animation and/or interaction. A spinning Ferris wheel or water wheel is one such possibility.