Are you looking for a fun, free coding activity that you can do with your class? CodeMonkey has you covered in our newest blog column – Offline Coding Activities. You don’t need any coding experience to carry-out this activity – simply read the following exercise and then do it tomorrow in class! This week’s 25 minute-long activity will help students understand how loops work in programming.
Step 1 – 10 mins.
Discuss the following with your students:
Programming is not only about writing the correct statements in the correct order, it is also about knowing how to write clear and short code.
Imagine that we have to write a simple program to make the monkey climb up a high staircase of 100 steps and we can only use the function “stepUp” to make the monkey climb up one step at a time.
Ask your students: “Do you think that the programmer wrote a line of code for every stair-step?” Just imagine how LONG this code would be ! It would be 100 lines of code!
So, instead of code that looks like this (X100):
stepUp stepUp stepUp etc…
Wouldn’t it be great to write something shorter? Ask your students to suggest a shorter way. How about something like this?
stepUp 100 times
Luckily, this is possible. Not exactly the way we just wrote it now, but quite similar. Code that is written in such a way is called a loop.
Step 2 – 5 mins.
Explain the following:
Explain to your class that a “simple loop” is a sequence of instructions that repeats a specified number of times. There are also other kinds of loops (for loops, until loops) that last until a particular condition is met, but we will learn about those later on.
Back to the staircase example…the way to write that in Coding Adventure would be:
100.times -> stepUp
The number 100 represents the number of times that we want the code inside the loop to run for.
Note the special syntax: the dot between the number and the word times, the space before
the ->, and the indentation of the code inside the loop (stepUp is the code inside the loop). Make sure your students know how to use the “Tab” key on their keyboards in order to get indentation into the code. Another alternative is to press the spacebar four times.
Remember that you can click the “times” button at the bottom in order to get a loop into the code without having to worry about the syntax.
Step 3 – 10 mins.
Offline Activity to do in class:
Let’s show another example to better clarify the use of a simple loop. Write the following code on the left-hand side of the board:
step 10 turn left step 10 turn left step 10 turn left step 10 turn left
Ask your students to identify a repeating pattern in the code. The pattern they identify should be:
step 10 turn left
Now, next to that code, on the right-hand side, write:
4.times-> step 10 turn left
Ask your students what they think each block of code does.
Explain that each code is the same; only the right-hand side of the code is written as a loop. Once we found the pattern on the left hand side, all we had to do was just write it once, and add 4.times->. The resulting code does the same, but is shorter and easier to understand.
The meaning of the code on the right is that “step 10, turn left” would repeat four times, and then the loop would be over. Once the loop is over, the computer moves onto the next statement.
This offline coding activity is from Lesson 5 in the Coding Adventure Part 1: Fundamentals lesson plans. For more fun activities and a detailed curriculum guide on how to teach CodeMonkey, sign-up for our free teacher trial here.