No matter which language you choose, you will certainly need some kind of looping logic. Here follows one of the simplest ways to understand how to understand the need for a loop.
In this tutorial:
See also:
1. Starting point
In the tutorial Learning to program with LOGO, we created the following program:
fd 100 rt 90 fd 100 rt 90 fd 100 rt 90 fd 100 rt 90
The program consists of a set of steps that results in the Turtle drawing a square. The program works and there is nothing wrong with it.
The clue to the next step is that we can see a pattern in the code.
2. “Eat, sleep, rave, repeat“
The pattern we can easily see is repetition of the first 2 lines of code: the fd
command is followed by an rt
command each time, and these two commands are repeated in this sequence 4 times.
Repeat the following two steps four times: go forward 100 steps and turn right.
Let’s start by deleting lines 3 to 8:
fd 100 rt 90
Now we need to find a way to repeat the remaining 2 lines in LOGO. The command we need is the repeat
command:
repeat 4 [ fd 100 rt 90 ]
3. Refactor
The program in Step 1 above and the final program in Step 2 are functionally identical even though the code has been rewritten. We say that the code has been refactored; Wikipedia defines refactoring as follows:
In computer programming and software design, code refactoring is the process of restructuring existing computer code—changing the factoring—without changing its external behaviour. Refactoring is intended to improve the design, structure, and/or implementation of the software, while preserving its functionality.
Wikipedia [2]
4. Next steps
But let’s be honest, I don’t think there is much of a market for a program that can only draw a square of one size. We are going to have to improve on it somehow…
The first step to doing this is going to be learning how to do something which at first will seem to be a waste of time: we are going to Variabilise a program in LOGO.
References:
- Turtle Academy – Lessons. (2023). Retrieved 24 January 2023, from https://turtleacademy.com/lessons/11
- Code refactoring. (2022, November 13). Retrieved 24 January 2023, from Wikipedia. https://en.wikipedia.org/wiki/Code_refactoring
- Terrapin. (No date) Commands Overview. Available at: https://resources.terrapinlogo.com/weblogo/commands/ (Accessed: 22 April 2024).