5. Drawing Your First Shape: A Perfect Square
- Drawing Your First Shape: A Perfect Square
A square has four equal sides and four equal corners (90-degree turns). Knowing this, we can command the turtle to draw one. Here is the “long way” to do it:
fd 100
rt 90
fd 100
rt 90
fd 100
rt 90
fd 100
rt 90
Notice a pattern? You’re repeating the exact same two commands—fd 100 and rt 90—four times. In programming, repeating yourself is a sign that there’s a smarter way to work.
Logo provides a powerful shortcut called the repeat command. This is your first introduction to a core programming concept called a loop. A loop tells the computer to perform a set of actions a specific number of times.
Here is the efficient way to draw the same square:
repeat 4 [fd 100 rt 90]
Let’s break this down:
- repeat 4: This tells Logo to repeat the following instructions 4 times.
- [fd 100 rt 90]: These are the commands inside the square brackets that will be repeated.
Using repeat saves a lot of typing. More importantly, it ensures every side and every turn is exactly the same. If you typed out the commands eight times and made a single typo on line seven (rt 89 instead of rt 90), your square would be broken! A loop guarantees perfection every time.
Great job! You’ve just written a real computer program. Now let’s explore a few more useful commands to manage your creations.