Minds On

Thinking like a robot

Imagine you are a robot and don’t know what a square is. You’ve asked your friend to give you specific instructions that will help you move in a square!

What might they say to you?

Are there any instructions that are repeated?

Record your answers using a format of your choice, then press ‘Hint’ to reveal and check your work.

One possible solution might look like the following code.

This is written in pseudocode:

  • Repeat 4
  • Forward 3
  • Quarter Right Turn
  • End

Student Tips

Did you know?

Pseudocode is used by computer programmers when planning their code.

They write in a language that is more similar to our everyday language, but in a way that can be translated to any programming language.

Record answers to the following questions using a format of your choice

  • Were the instructions that you came up with like the pseudocode above?
  • How were they different? Similar?
  • Would you alter your instructions now that you’ve seen this pseudocode for walking in a square? Explain your thinking.

Action

Coding with Scratch

Task 1

Examine the following code needed for drawing a square in Scratch. An arrow is used to display the movement of the code in this example.

The sequence of code is as follows: When the flag is pressed to start the program, the sprite will be pointed in direction 90, then the arrow will go to the point on the screen where x is at 0 and y is at 0, all previous pen markings are erased on the screen and the pen is down to mark the motion of the arrow. The following 4 actions are then repeated 4 times: (1) move 100 steps, (2) wait 0.25 seconds, (3) turn 90 degrees and (4) wait 0.25 seconds. After that the code has been executed completely.

The top part resets everything so that the arrow:

  • faces in the right direction
  • moves to the middle of the screen
  • clears anything that was previously written on the screen
  • puts the bug’s pen down

The bottom part (where the repeat begins), will move the bug in the shape of a square. Squares are made up of right angles.

90 degrees makes a quarter turn.

90 degrees is also called a right angle.

Try the code by yourself! Press Scratch(Opens in a new window) to access the code. Click the green flag at the top to run the code!

Now let’s try altering the code in the following ways. Describe what happens to each outcome when you run the code by clicking on the green flag. Record your observations using a method of your choice.

  1. Change the move 100 steps to move 50 steps.
Learner Facing

The sequence of code is as follows: When the flag is pressed to start the program, the sprite will be pointed in direction 90, then the arrow will go to the point on the screen where x is at 0 and y is at 0, all previous pen markings are erased on the screen and the pen is down to mark the motion of the arrow. The following 4 actions are then repeated 4 times: (1) move 50 steps, (2) wait 0.25 seconds, (3) turn 90 degrees and (4) wait 0.25 seconds. After that the code has been executed completely.

2. Change the Repeat from 4 to 2.

The sequence of code is as follows: When the flag is pressed to start the program, the sprite will be pointed in direction 90, then the arrow will go to the point on the screen where x is at 0 and y is at 0, all previous pen markings are erased on the screen and the pen is down to mark the motion of the arrow. The following 4 actions are then repeated 2 times: (1) move 50 steps, (2) wait 0.25 seconds, (3) turn 90 degrees and (4) wait 0.25 seconds. After that the code has been executed completely.

Alter the code

Alter one part of the code yourself. Predict what will happen to the outcome before running the code. What did you alter?

Run the code. How did this change the outcome?

Task 2

Let’s put a “nested” event into our code to change the image drawn!

Nested events are control structures that are placed inside other control structures; for example, loops occurring inside other loops.

Loops are also called repeats.

Press Scratch(Opens in a new window) to access the code. Click the green flag at the top to run the code and find out what happens!

You can observe that in this code, we have a repeat of 4 inside of another repeat of 12.

Before the second square is drawn, the bug rotates 30 degrees.

Student Tips

Did you know?

30 degree is an acute angle because it is less than 90 degrees.

The sequence of code is as follows:

When the flag is pressed to start the program, the sprite will be pointed in direction 90, then the arrow will go to the point on the screen where x is at 0 and y is at 0, all previous pen markings are erased on the screen and the pen is down to mark the motion of the arrow. Then the following is then repeated 12 times by the sprite:

  1. Repeat the following 2 actions 4 times (this is within the first repeat): move 100 steps and turn 90 degrees.
  2. After the nested loop that repeats 4 times, turn 30 degrees to complete the initial loop.

After that the code has been executed completely.

Try these

Alter the following code and describe how the changes affect the outcome. Record your observations in a method of your choice.

  1. Change the outer repeat to 6 and the turn 30 degrees block to 60.

The sequence of code is as follows: When the flag is pressed to start the program, the sprite will be pointed in direction 90, then the arrow will go to the point on the screen where x is at 0 and y is at 0, all previous pen markings are erased on the screen and the pen is down to mark the motion of the arrow. Then the following is then repeated 6 times by the sprite: (1) Repeat the following 2 actions 4 times (this is within the first repeat) (1) move 100 steps, (2) turn 90 degrees. (2) After the nested loop that repeats 4 times, turn 60 degrees to complete the initial loop. After that the code has been executed completely.

  1. Change the outer repeat to 36 and the turn 60 degrees block to 10 degrees.

The sequence of code is as follows: When the flag is pressed to start the program, the sprite will be pointed in direction 90, then the arrow will go to the point on the screen where x is at 0 and y is at 0, all previous pen markings are erased on the screen and the pen is down to mark the motion of the arrow. Then the following is then repeated 36 times by the sprite: (1) Repeat the following 2 actions 4 times (this is within the first repeat) (1) move 100 steps, (2) turn 90 degrees. (2) After the nested loop that repeats 4 times, turn 10 degrees to complete the initial loop. After that the code has been executed completely.

Notice

Complete the following fillable and printable document Product Table. You can also complete this activity in your notebook or using the method of your choice.

Code Outer Repeat Turn degrees (in between each repeat) Product of these two numbers

Original Code

12

30

Change 1

6

60

360

Change 2

36

10

Press the ‘Activity’ button to access Product Table. 

What do you notice about the product (the answer to a multiplication question) of the sets of numbers used so far in our code, that contain nested events?

  1. Now that you know the outer repeat, and the turn block in between the two repeats need to make a product of 360, change the repeat, and turn block in between the two repeats to make a new version of the circle of squares.
    • The values have been left blank in the following code.
    • Use numbers other than the ones we have used so far.
    • You can use any two numbers, as long as their product is 360.

The sequence of code is as follows: When the flag is pressed to start the program, the sprite will be pointed in direction 90, then the arrow will go to the point on the screen where x is at 0 and y is at 0, all previous pen markings are erased on the screen and the pen is down to mark the motion of the arrow. Then the following is then repeated (blank) times by the sprite: (1) Repeat the following 2 actions 4 times (this is within the first repeat) (1) move 100 steps, (2) turn 90 degrees. (2) After the nested loop that repeats 4 times, turn (blank) degrees to complete the initial loop. After that the code has been executed completely.

What two numbers did you use and how did it change the outcome?

Consolidation

What’s the pattern?

Observe the following code. What is the resulting outcome when the green flag is pressed?

The sequence of code is as follows: When the flag is pressed to start the program, the sprite will be pointed in direction 90, then the arrow will go to the point on the screen where x is at 0 and y is at 0, all previous pen markings are erased on the screen and the pen is down to mark the motion of the arrow. Then the following is then repeated 3 times by the sprite: (1) Repeat the following 2 actions 4 times (this is within the first repeat) (1) move 100 steps, (2) turn 90 degrees. (2) After the nested loop that repeats 4 times, turn 120 degrees to complete the initial loop. After that the code has been executed completely.

Think about your learning

  • What are some important things to keep in mind when you are reading and altering code that involves sequential, repeating, and nested events?
  • What strategies do you use when predicting the outcome after code has changed?
  • Are your predictions accurate? If they are, why do you think that is? If they aren’t, how could you change your strategy to make them more accurate?

Reflection

As you read through these descriptions, which sentence best describes how you are feeling about your understanding of this learning activity? Press the button that is beside this sentence.

I feel...

Now, record your ideas using a voice recorder, speech-to-text, or writing tool.