Day 49 (WC)

Unit 3, Chapter 1, Lesson 9: Looping and Random Numbers


Students learn to use random values and looping to create variation in their drawings and quickly duplicate objects they wish to appear in their digital scenes many times. Students will be presented with a version of the for loop which only enables them to change the number of times the loop runs. This block is essentially a “repeat” block and will be presented that way. Students will also be presented with blocks which enable them to choose a random number within a given range. Together these blocks enable students to create more complex backgrounds for digital scenes by randomly placing simple objects within the background of their scene. Students use these tools to step through the Under the Sea exemplar digital scene.


Loops are a relatively straightforward idea in programming – you want a certain chunk of code to run repeatedly – but it takes a little practice to get good at controlling loops and recognizing how and where in your programs to use them. The for loop in JavaScript (and many other programming languages) is designed to be used for both simple and sophisticated programming tasks, thus it has a lot of syntax to it that will be explained in the future. In this lesson, the block-based form of the for loop exposed to students is effectively a simple repeat loop – it only lets them change a number that dictates how many times the loop repeats.

Random numbers are also used more (much more) in this lesson as an effective way to experiment with loops. Creating some randomly-generated output with each iteration of the loop is good visual feedback that the loop is running the way you expect. It also helps you explore the ranges of possible outputs, which tells you more about what your program can and cannot do.


Students will be able to:

  • Use a loop in a program to simplify the expression of repeated tasks.
  • Identify appropriate situations in a program for using a loop.
  • Use random values within a loop to repeat code that behaves differently each time it is executed.


  • For Loop – Loops that have a predetermined beginning, end, and increment (step interval).
  • Loop – The action of doing something over and over again.

New Code

Getting Started

  1. Loops versus Functions–As we have developed as programmers, we have focused on the process of breaking down large tasks into smaller pieces and assigning each piece a function.
  2. When we break down a large task, often we will find that some portion of the task needs to be repeated many times. As programmers, we would simply call the same function many times. This might work if we need to call the same function five times, but if that function needs to be run 1,000,000 times, we’ll need a better solution.
  3. Today we’ll be exploring how a programming construct called a loop solves this problem by allowing us to repeat a set of commands many times.
  4. We’ll also practice looping through commands many times with random input, therefore giving us many instances of random output.
  5. Just as we saw yesterday, this will be both useful for testing our code and also for developing more variety in our drawings.
  6. At the end of class we’ll ask: “When should you use a loop instead of a function, or vice versa?”


  1. Today we’ll learn how to use something called a “for loop”
  2. The for loop is a JavaScript programming construct – it looks and works the same here as it does in “the real world”.
  3. Log in to
  4. Work through the lesson on your own–be sure to READ EVERYTHING!!
  5. Evaluate at least 1 program someone else did. Include the name of the person whose code you critiqued. Use the following criteria:
    1. The program draws the diamond
    2. The program defines four functions: right(); drawStep(); drawSide(); and drawDiamond(); The names are less important than the existence of four functions with this functionality.
    3. The program makes a single call to drawDiamond();
    4. The program looks clean and organized


  1. When to use loops versus functions–Having used loops to repeatedly run a smaller portion of a larger program, students should attempt to formalize their understanding of when a loop should be used.
  2. In your Word Online file for this class, respond to:  Develop a rule for deciding when to use a loop within a program. Perhaps think about when to use a loop versus a function. Try to make connections to Top-Down Design in your response. Below your rule, write a couple sentences justifying your rule.
  3. Important points to draw out are related to when to use a function v. when to use a loop. While you can get code to function similarly with a function or a loop the general rule of thumb is:
    • Write a function when you have a piece of code – a procedure – that you might reuse in other places in your program.
      • A function might also help you encapsulate a solution to a common problem in one place that you can call on repeatedly with different inputs (e.g. drawSquare(size))
      • Writing a function is often more of a design decision. You are trying to encapsulate and abstract details so you think about other parts of a larger program
    • Write a loop when there is something you need to do over and over again and it doesn’t make sense to split it up any more.
      • Writing a loop is more of an algorithmic decision – you actually just need repetitious behavior to solve some problem.
      • For example, we don’t want to manually call functions many times in a row. If you’re calling the same function many times in a row, it’s time to make a loop.
  4. In your Word Online file for this class, respond to the following:
    1. When breaking a problem down, you often encounter elements that you want to use repeatedly in your code. Sometimes it’s appropriate to write a new function; at other times it’s appropriate to write a loop. There is no hard-and-fast rule as to which is better, but what do you think? What kinds of circumstances would lead you to writing a function versus using a loop?

Leave a Reply

Your email address will not be published. Required fields are marked *