Unplugged: Walk a square

Chair with Pseudocode on the board.

Your Task

In this activity, you’ll guide yourself step-by-step through a simple exercise, then figure out how to make your instructions shorter and more efficient by using a loop.

WHat You’ll Do

  • Place a chair in front of you.
  • Stand at the back right side of the chair, facing forward.
  • Now, think about what instructions you would need to give yourself in order to walk around the chair and end up exactly where you started. If you’re not sure, try walking around the chair once to get an idea of the steps.

##Writing the Instructions

  1. Step forward
  2. Turn left
  3. Step forward
  4. Turn left
  5. Step forward
  6. Turn left
  7. Step forward
  8. Turn left Square walking pattern

That’s eight lines of instructions for you to follow!

Make It More Efficient

Now, look at the instructions you wrote. Do you notice any steps that repeat?

Hint: What actions do you keep doing? (1) Step forward. (2) Turn left. How many times do you repeat these steps? (Answer: Four times).

Here’s the key: Whenever you see repeated steps, you can use a loop to simplify your instructions. How could you rewrite your directions? It might look like this:

Repeat 4 times: Step forward, Turn left

Test the New Instructions

Now, follow your new, shorter set of instructions. Did it work? Great! You’ve just taken eight lines of instructions and turned them into three by using a loop!

Why Fewer Instructions Matter

Think about why using fewer instructions can be helpful. Some reasons could be:

  • It saves time
  • It’s less to write down
  • There’s less chance of making mistakes
  • It’s easier to read
  • It’s faster to find and fix problems

You’ve just learned how to make your instructions (or code) more efficient by using loops!