4. Module 3: Expanding Computational Thinking
- Module 3: Expanding Computational Thinking
To create programs that can truly adapt and respond to changing conditions, students must learn how to implement decision-making logic. This module introduces the structures that allow a program to make choices and the powerful concept of recursion. Mastering these skills is critical for elevating a student’s code from a static script to a dynamic and intelligent program.
4.1. Decision-Making in Logo
Logo provides structures for creating programs that can repeat actions until a condition is met or execute commands only if a condition is true.
The while loop allows a block of code to be executed repeatedly as long as a specified condition remains true. To create a procedure that draws an expanding spiral, enter this code into the editor:
to spiral
make “n 1
while [:n < 100] [
make “n :n + 5
fd :n
rt 90
]
end
In this example, the while [:n < 100] structure establishes the condition: the loop will continue as long as the value of :n is less than 100. With each iteration, :n increases, until the condition is no longer met and the loop terminates.
The if statement provides another form of decision-making, executing a block of code only if a specific condition is met. The “random walk” algorithm illustrates how if can be used to create unpredictable behavior. To define a procedure for a random walk, enter this code:
to random_walk
make “r random 3
if :r = 0 [fd 20]
if :r = 1 [rt 90 fd 20]
if :r = 2 [lt 90 fd 20]
end
Here, the random 3 command generates an integer (0, 1, or 2). A series of if statements then checks this value and directs the turtle to move forward, turn right, or turn left based on the random outcome.
4.2. The Power of Recursion
A recursive procedure is one that calls itself from within its own code. This elegant and powerful concept is fundamental to computer science and allows for the creation of complex, self-similar patterns like fractals and spirals.
The following spiral_recur procedure is a classic example of recursion. To define it, enter this code into the editor:
to spiral_recur :n
if :n < 1 [stop]
fd :n
rt 20
spiral_recur 0.95 * :n
end
The logic can be deconstructed as follows:
- to spiral_recur :n: The procedure takes a size argument, :n.
- if :n < 1 [stop]: This is the crucial stop condition (or base case). It prevents the procedure from calling itself forever. Once :n becomes too small, the procedure stops.
- spiral_recur 0.95 * :n: This is the recursive call. The procedure calls itself, but with a slightly smaller argument. This repeated process creates the spiral effect.
Educator’s Note: The stop condition is the most important part of teaching recursion. An infinite recursion will cause the program to crash. Have students experiment by removing the if :n < 1 [stop] line to see what happens. This provides a memorable lesson on the importance of base cases in recursive algorithms.
Now that students can create programs with sophisticated logic, the next module focuses on adding aesthetic and creative elements to their projects.