Minds On

Introduction to coding

Children learning to code in the classroom using devices

Coding using a computer involves writing instructions.

In coding, there is a way to tell a computer to repeat instructions without writing a separate line of code for each repeated instruction.

We can use what is called a repeat, or a loop.

Repeating events

Repeating events are something that happens over and over again.

Explore the instructions for a workout routine below:

In the left column, repeats are used, which means the code requires fewer lines of code. In the second column on the right, the same code is written, but without repeats.

Notice that this workout routine is written like code – with step-by-step instructions, and a repeat is used to indicate how many times to do some of the movements to have a code with fewer lines.

Student Success

Think-Pair-Share

Let’s write our own instructions for a dance or workout routine.

If possible, work with a partner to write your own instructions for a routine:

  1. First, start by deciding if you will design a dance or a workout routine.
  2. Then, write step-by-step instructions for your dance or workout routine. Remember that some of the instructions must be repeated.
  3. Lastly, include repeats in your instructions.

Record your ideas in your notebook or another method of your choice.

Note to teachers: See your teacher guide for collaboration tools, ideas and suggestions.

Action

Coding with Scratch

Introduction

Now that we’ve created instructions for a dance or workout routine, let’s write some code for a routine on our computer using Scratch.

Some code is already written for you. Press Scratch(Opens in a new window) to access the code. Click the green flag at the top to run the code!

Student Tips

Helpful Scratch tips

Here are a few key tips while using Scratch:

  • The green flag at the top of the screen will execute (run) all the code that contains the “when green flag clicked” block.

This is a screenshot from Scratch. At the top of the screen there is a green flag next to a red octagon. The green flag is circled to indicate that pressing on it will run the code. On the left side there are code instructions for a dance routine. On the right side are photos of two “sprites” or characters, Cassie and Champ, who appear to be dancing. Below the characters, there is a setting control panel where the user can select a character and change their size, direction, and position.

  • In this case, we have two concurrent events happening – both characters (sprites) are dancing at the same time.
  • You can click on each character (sprite) to see their code – there is separate code for Cassy and Champ.

This is a screenshot from Scratch. At the top of the screen there is a green flag next to a red octagon. On the left side there are code instructions for a dance routine. On the right side are photos of two “sprites” or characters, Cassie and Champ, who appear to be dancing. Below the characters, there is a setting control panel where the user can select a character and change their size, direction, and position. Beneath the setting controls, there are 2 small squares. 1 square shows a photo of Cassie and the other shows a photo of Champ. Cassie’s photo is circled to indicate that when this square is pressed, Cassie’s dance code instructions will appear on the left side of the page.

This is a screenshot from Scratch. At the top of the screen there is a green flag next to a red octagon. On the left side there are code instructions for a dance routine. On the right side are photos of two “sprites” or characters, Cassie and Champ, who appear to be dancing. Below the characters, there is a setting control panel where the user can select a character and change their size, direction, and position. Beneath the setting controls, there are 2 small squares. 1 square shows a photo of Cassie and the other shows a photo of Champ. Champ’s photo is circled to indicate that when this square is pressed, Champ’s dance code instructions will appear on the left side of the page.

Explore the code!

Run the code by selecting the green flag at the top of the screen. Both characters should begin their dance routine at the same time.

Select the correct answer, then press ‘Check Answer’ to see how you did.

Task 1: Change the code

  1. Change the length of the repeat, (it currently has a value of 6), for Cassie to make her dance last for the same length as Champ’s dance.

This is a screenshot of the code for Cassie’s dance routine. The code begins with the text “when green flag clicked”. The next line of code indicates the sprite will switch costume to “move_a” then wait 0.25 seconds. Next, the sprite will switch costume to “move_b,” then wait 0.25 seconds. Next, the following sequence of dance moves is set to repeat 6 times: First, switch costume to “move_c,” then wait 0.25 seconds. Next, switch costume to “move_d,” then wait 0.25 seconds.

  1. Run the code by selecting the green flag at the top of the screen.
  • Reflect on the following questions:
  • Did the change you made in your code result in the intended outcome?
  • Do Cassie and Champ dance for the same length of time?

If not, change the repeat in Cassie’s code until the two characters dance for the same length of time.

  1. Add another repeat to each of the code sequences. You will need to go to the Control tab on the left-hand side to find the “repeat block.”

Place it around the dance moves (the blocks that start with “switch costume to… ”) you would like to repeat.

This is a screenshot of the control blocks in Scratch. There are tabs along the top of the screen. From left to right, these tabs are titled Code, Costumes, and Sounds. The Code tab has been selected. On the left sidebar, under the code tab, there are circles labelled with the different code categories. From top to bottom, the categories are looks, sound, events, control, sensing, operators, variables, and my blocks. The circle labelled “control” is selected. A column of 5 different control blocks appears to the right of the code categories. The circled block in the image is the second block from the top, which is the repeat block. The instruction shown in this block is “repeat 10”.

Student Tips

Reminders

  • When using Scratch, it may take some time to get used to moving blocks around the screen.
  • You can always click on the teacher provided link to go back to the starting code.
  • If you would like to add additional dance moves to your sprites, you can find the “switch costume to” block in the Looks option on the left-hand side.

This is a screenshot of the Looks blocks in Scratch. There are tabs along the top of the screen. From left to right, these tabs are titled Code, Costumes, and Sounds. The Code tab has been selected. On the left sidebar, under the code tab, there are circles labelled with the different code categories. The circle labelled “looks” is selected. A column of 8 different looks blocks appears to the right of the code categories. The circled block in the image is the fifth block from the top, which is the “switch costume to…” block.

Task 2: Predict and run the code

  1. Predict what will happen now that you’ve altered the code, before clicking on the green flag to run the code!
  2. Run the code by clicking on the green flag!
  3. Reflect on the following questions:
    • Did the sequences run the way you expected?
    • Do you need to make more changes?
  4. (Optional) Try adjusting the code to change the dance sequences to your own liking.

Consolidation

New additions

Let’s add a third character to our dancing party.

  1. Go to the bottom right of your screen and select the blue icon with “Choose a Sprite.” This icon looks like a little cat with a + sign. By clicking the icon, it will take you to a new page with the title “Choose a Sprite.”
  2. Beneath the title, there are many sprite options, like animals, people, fantasy, etc. Click on the Dance option at the top.
  3. If you hold your mouse over each sprite, you should see them demonstrate all of their dance moves.

This is a screenshot of the “Choose a Sprite” page in Scratch. There are tabs along the top of the screen. These tabs are: All, animals, people, fantasy, dance, music, sports, food, fashion, and letter. The selected tab is Dance. Under the Dance tab, there are 9 different sprites or characters. From left to right, the sprites are Amon, Anina Dance, Ballerina, Cassy Dance, Champ99, D-Money Dance, Jouvi Dance, LB Dance, and Ten80 Dance.

  1. Select the sprite you wish to use.
  2. Write code for a new dance sequence for the new dancer.

Scratch tips with blocks and tools

Here are some tips for where you can access the required blocks and tools. Press an item to reveal the tip.

You can find this beneath the stage, where the sprites are. The size starts at 100. Use a smaller number to make the sprite smaller. For sprite size, access control settings below sprites and insert number. Size box is circled.

To add the “When the green flag clicked” block, explore the Events option (left-hand side) under the Code tab (near the top). Under the Code tab, in the Events option, circled is the first block: “When the green flag clicked.”

To add a “Switch costume” block, explore the Code tab (near the top), under the Looks option (left-hand side).Under the Code tab, in the Looks option, circled is the fifth block: “Switch costume to.”

To add a “Wait” block, explore the Code tab (near the top), under the Control option (left-hand side). The block is the first in the list. Insert a number into this block, which will make the dance movement stay on the screen for as long as you choose. Under Code, in the Control option, circled is the block: “wait.” Insert number for how long to wait.

To add a “Repeat” block, explore the Code tab (near the top), under the Control option (left-hand side). The block is the second on the list. Insert a number into this block, which will make the selected dance movement(s) to repeat on the screen for as long as you choose. Under Code, in the Control option, circled is the block: “repeat.” Insert number for how many times.

Making adjustments

You can always click on the other sprites to check out the code we used for them.

Make the dance sequence of your third dancer last the same length of time as our first two dancers! Remember to adjust the repeats as necessary.

Explore another Scratch code with a third dancer, Anina. Press Scratch(Opens in a new window) to access the code. Anina has been added as an additional sprite, but there is no developed code for Anina’s dance routine.

Reflection

How do you feel about what you have learned in this activity? Which of the next four sentences best matches how you are feeling about your learning? Press the button that is beside this sentence.

I feel…

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