CPSC 120 Principles of Computer Science Fall 2025

Lab 8
Repetition, Again

Due: Fri 11/7 at the start of class


Introduction

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:


Handin and Presentation Meeting

Handin

Hand in a hardcopy (paper) of your worksheet in class.

To hand in your sketches:

It is OK if you copy your files to the handin directory at the very beginning of class.

Presentation Meeting

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.


Policies

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.


Preliminaries

Decomposition Into Loops

There are three main patterns of repeated elements:

Reference

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.


Exercises

  1. 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:

    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.

  2. In this exercise you'll create a sketch which draws a tumbling blocks quilt, as shown.

    The requirements for your sketch:

    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.

  3. 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:

    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.

  4. 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:

    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).

Extra Credit

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: