Unit 3 Loops, Arrays, Media
How can sounds, images, and fonts can be combined and manipulated with code?

1.2 For Loops I

How can we use iteration to abstract artwork?


Overview

This learning activity introduces repetition using for loops. Students will alter their code from the previous lesson to demonstrate an understanding of the structure of a for loop. They will change the values inside the for loop to achieve different results in their design.

Suggested Duration

45 minutes

Objectives

Students will be able to:

  • Explain how the components of the while loops are essential to efficiency
  • Create for loops
  • Use for loops to generate multiple shapes
  • Explain the difference between while and for loops

Student Outcomes

Abstraction:

  • Describe different things I tried in order to achieve a goal.

Algorithms:

  • Explain why I used specific instructions to complete a task.
  • Compare and contrast my instructions with other instructions that complete the same task.
  • Demonstrate the benefit of using an event, conditional or loop in my prototype.

Prototype:

  • Experiment with the commands of a programming language.
  • Explain why I chose specific commands to communicate my instructions.

Vocabulary

Loops A sequence of instructions that is repeated until a certain condition is met.
Iterations The repetition of a process or utterance.
For Loop The for loop loops through a block of code a specific number of times

Resources

For Loops

Repeating a block of code over and over until a condition is met is such a common system in programming that a shorthand for the while loop was created that puts the three steps together. It's called a for loop, and its syntax is as follows:

Notice that the for loop contains the same three elements as a while loop but they are written in a single line. A for loop is basically shorthand for a while loop and once you get the hang of them, they're really useful. Here are our five ellipses from the previous lesson, drawn using a for loop. Notice how we no longer have repeated lines in our code. Play with the values to see how the design changes.

Exercise #1: Draw two rows of any shape. Use a while loop to draw the first row and a for loop to draw the second.

Change the y location so that they don't overlap, and you can change the color as well. Notice how the result is the same, but the code used in a for loop is more concise.

Exercise #2: Return to your designs from the previous assignment and repeat your design with a different Y value using a for loop.

Exercise #3: Play with for loops

For loops take some practice. Remove the while loop from your code and play with the variables inside the for loop to achieve different results. Pay attention to what each element in the loop controls.

In this example, I initialized the x variable as 0 so the row begins on the left side of the canvas, incremented the x location by 50 pixels so that they are drawn closer together, and only drew them on condition that x is less than or equal to width/2 so that they are only drawn on the left half of the canvas.

Notice how easy it is to create different versions of the same design. Imagine how long it would take to change the spacing, size, and location of multiple shapes if we programmed each one individually.