Overview

The students will learn to use random values and looping to create variation in their drawings with Quorum Turtle Module (QTM), and quickly duplicate objects they wish to appear in their digital scenes many times. The students will also be presented with a version of the "repeat ? times," which enables them to change the number of times the loop runs. This block is essentially a "repeat" block and will be presented that way. The students will also be presented with blocks which enable them to choose a random number within a given range. Together these blocks will enable them to create more complex drawings for digital scenes by randomly placing simple objects within the drawings. They use these tools to create a variety of drawings with QTM.

Vocabulary

Goals

Students will be able to:

Purpose

Loops are a relatively straightforward idea in programming - used when someone wants 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 the programs to use them. The "repeat" in the Quorum Programming Language 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 simplest type of the "repeat" is introduced - it only lets them change a number that dictates how many times the loop repeats.

Random numbers are also used 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 and auditory feedback that the loop is running the way that someone would expect. It also helps the students explore the ranges of possible outputs, which tells them more about what their program can and cannot do.

Tips

In Quorum Programming Language, the concept of "loops" is represented simply by the keyword "repeat." Quorum Language is also known as the world's first "Evidence Oriented Programming Language." This means that we did research before we decided what keyword we should use for certain actions and concepts of computer programming. When we researched the concept of "loops," more programmers - novice level and expert level - understood this concept by using the keyword "repeat" instead of the word "loop," but students should be understand that both terms refer to the same thing.

Getting Started

New Code

Loops versus actions

These past lessons have focused on the process of breaking down large tasks into smaller pieces and assigning each piece an action. When the students break down a large task, often the students will find that some portion of the task needs to be repeated many times. As programmers, the students could simply call the same action many times. This might work if someone needs to call the same action five times, but if that action needs to be run 1,000,000 times, we will need a better solution for the student.

Today, the students will be exploring how a programming construct called a loop solves this problem by allowing them to repeat a set of commands many times. They will also practice looping through commands many times with random input. This will be useful both for testing the code and also for developing more variety in their drawings.

At the end of class we need to ask the students to think about the following: "When should you use a loop instead of a action, or vice versa?"

The students will have an opportunity to explore the process of creating loops by programming with them in Quorum Turtle Module (QTM). Use the Getting Started Activity to realize the need in some programs to repeat commands many times, and insight into the fact that random inputs can be used in repeated commands to rapidly develop a variety of behaviors.

Commenting in Code

The more sophisticated someone's code becomes, the more he or she have the chance to get himself or herself confused. This is because so many steps of abstractions and complexity management is created in the code that the steps themselves become the source of complexity. Also, when you is working on the same code with other programmers, you want them to be able to understand your code, so that the other programmers could have a chance to improve some parts of your code. For this reason, good programmers use commenting in their code.

In Quorum Programming Language, there are two ways of commenting. One way is to use the "//" (double slash) so that the rest of a line becomes a comment. The other way is to use "/*" at the beginning of a comment, then use the "*/" at the end of the comment. This method becomes handy if someone needs to use multiple lines for one comment string. The following is an example of both comment styles.

 use Libraries.Compute.Random
 integer counter = 0 //Using this variable for keep track of the number

 /*
 In the following part, I will use the mathematical formula:
 A^2 = B^2 + C^2
 */
end

Activity 1: Coding Challenge - Part 1

Pre-Activity: Video "Using Loops"

Teacher's Tip

In the following video, the concept of "Functions" is described using the "Turtle Programming tool" used in the Code.org curriculum. While the "Turtle Programming tool" primarily uses a "block language" (a language that could be written by combining picture blocks on the computer screen), the "Quorum Turtle Module" uses "text language" as the students have already experienced in the previous lessons. Although the style of the coding is different between the two languages, the concept that supports the execution of code is very similar. Therefore viewing this video before the students start the activity is still beneficial.

Challenge 18: Draw Steps

Setting up a "repeat […] times" in Quorum Programming Language

In the Quorum Programming Language, the concept of a loop is represented by the keyword "repeat." For example:

repeat 5 times

end

The students should write their code on the lines between "repeat 5 times" and "end." Students can write any number of lines as far as it logically make sense to do so. For example, the students can ask the turtle to move forward "x" number of steps using "MoveForward()" between a repeat structure:

action MoveForward(integer x)
   repeat x times
       MoveForward()
   end
end

Student Instructions

Description of an Image

The picture below shows steps going up drawn 5 times consecutively to the right of each other.

5 steps

Remember that you have learned how to draw a single step (Challenge 8 from Lesson 5)? Now, try to write an action that uses the repeat structure to draw 5 steps to the right.

Quorum Turtle Module: Challenge 18

When their code is working properly, they would see the following lines drawn: (0, 0) to (0, 1) to (1, 1); (1, 1) to (1, 2) to (2, 2); (2, 2) to (2, 3) to (3, 3); (3, 3) to (3, 4) to (4, 4); (4, 4) to (4, 5) to (5, 5).

Challenge 19: Make it easier to increase or decrease the number of the steps

Student Instructions

Now, extend what you have done in Challenge 18 to draw the number of steps of your choice.

Hint: remember what you can do with actions that have parameters passed in.

Quorum Turtle Module: Challenge 19

Challenge 20: Draw Square Steps

Student Instructions

Description of an Image

The picture below shows squares drawn like a steps going up from left to right 9 times.

Square Steps

Now based on what you have done in Challenge 19, try to recreate the square steps shown in the image.

Quorum Turtle Module: Challenge 19

When their code is working properly, they would see the following lines drawn: (0, 0) to (0, 1) to (1, 1); (1, 1) to (1, 2) to (2, 2); (2, 2) to (2, 3) to (3, 3); (3, 3) to (3, 4) to (4, 4); (4, 4) to (4, 5) to (5, 5); (5, 5) to (5, 6) to (6, 6); (6, 6) to (6, 7) to (7, 7); (7, 7) to (7, 8) to (8, 8); (8, 8) to (8, 9) to (9, 9); (9, 9) to (9, 8) to (8, 8); (8, 8) to (8, 7) to (7, 7); (7, 7) to (7, 6) to (6, 6); (6, 6) to (6, 5) to (5, 5); (5, 5) to (5, 4) to (4, 4); (4, 4) to (4, 3) to (3, 3); (3, 3) to (3, 2) to (2, 2); (2, 2) to (2, 1) to (1, 1); (1, 1) to (1, 0) to (0, 0).

Looping with Random Values

Student Instructions

This activity will be structured like a walkthrough that will help you to become more familiar with the Random Number Generator and using different actions to draw the image below.

Image Description

The image below shows a bar graph with different height of the lines drawn on each vertical column.

Bar Graph

Think of the code that makes the turtle to draw one line at a time. The turtle needs to go up a certain number of blocks while drawing lines. Then turn around and come down the same number of blocks while not drawing the lines. Finally, the turtle needs to move itself two columns right to the right and face upward.

Often we want to start with something simple that we can work on easily. So let's start with drawing one bar. Given the template code in the IDE, try to finish the DrawOneBar action using PenDown, RepeatMoveForward, TurnLeft, MoveForward, and other actions defined in the IDE.

Quorum Turtle Module: Looping 1

Now, try to put the whole structure into an action so that we can control how many bars are drawn. Let's call this action DrawBars(integer x). The parameter "x" would accept our request of "how many bars" to draw (keep the range of the turtle's movement in mind). Finish the code in the IDE below.

Quorum Turtle Module: Looping 2

Activity 2: Coding Challenge - Part 2

Using a Loop inside of the Loop

When the students successfully complete the following coding challenges, their program will have the turtle draw something similar to the picture below. The picture shows four banners with different pole heights and different banner sizes - either taller and shorter. These banners look like stacked up squares - each square has one block length for a side. Each pole location is separated by two columns of horizontal length. At the top of each pole, there is a banner with different height. The students' code will draw this pattern of pictures with different heights of the both poles and banners each time they run the same code.

Pole with Banners

Challenge 21: Draw a banner

Student Instructions

The goal for this challenge is to draw a banner which is a rectangle that looks like stacked up squares.

A banner

Set the starting coordinate of the turtle to (0, 0). Create an action that accepts an integer parameter for the height of the banner. You need to include the "repeat" structure for writing this code. Once you have created the action, run the action in Turtle Commands with number "4" as the parameter.

Hint: In the description of the image above we said the banner looks like stacked up squares.

Quorum Turtle Module: Challenge 21

When their code is working properly, they will see the following lines drawn: (0, 0) to (1, 0); (1, 0) to (1, 1) to (0, 1) to (0, 0); (1, 1) to (1, 2) to (0, 2) to (0, 1); (1, 2) to (1, 3) to (0, 3) to (0, 2); (1, 3) to (1, 4) to (0, 4) to (0, 3).

Challenge 22: Draw Poles on Every Other Column

Student Instructions

Now, try to draw poles on every other column of the grid, which is similar to how we drew the bar graph before.

Quorum Turtle Module: Challenge 22

Challenge 23: Putting the two together

Student Instructions

Now, combine the code from Challenges 21 and 22. The idea is, at the top of the pole drawing, the turtle will switch tasks to draw a banner. You don't have to use random number generator for this one. So, create an action that accepts one integer value for the height of the pole and one integer value for the height of the banner and has the following sequence:

  1. The turtle goes up a "set length of pole height" while drawing the lines.
  2. The turtle starts drawing the banner for "a set height" while drawing the lines, then goes back to the top end of the pole by moving down "a set height distance."
  3. The the turtle goes down the pole for "the set length distance" to the bottom row.
  4. Once at the bottom row, the turtle moves itself two columns to the right and turns to face upward.

After you have done creating the action, pass in 4 as the parameter for the pole height and 3 for the banner height.

One pole with a banner

Quorum Turtle Module: Challenge 23

Assuming students started the turtle from dot (0, 0), they should see the following lines drawn: (0, 0) to (0, 1) to (0, 2) to (0, 3) to (0, 4); (0, 4) to (1, 4); (1, 4) to (1, 5) to (0, 5) to (0, 4); (1, 5) to (1, 6) to (0, 6) to (0, 5); (1, 6) to (1, 7) to (0, 7) to (0, 6).

Challenge 24: Incorporate randomness

Student Instructions

For this final challenge, you should create an action that runs the completed code for challenge 23 four times. However, your code needs to produce the randomly generated height of the pole and randomly generated height of the banner for every separate "Pole and Banner" drawing. For the practical limitation of the height in the Turtle Module, have the random integer generator generate integers between 1 and 4 for both values.

Quorum Turtle Module: Challenge 24

Wrap Up

Discussion: When to use loops versus actions

Develop a rule for deciding when to use a loop within a program. Perhaps think about when to use a loop versus an action. Ask the students to try to make connections to Top-Down Design in their response. Below their rules, they should write a few sentences justifying their rule.

Discussion Goal

It is important for students to distinguish between when to use an action and when to use a loop:

Assessment

Free Response

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?

Standards Alignment