3. Module 2: Introducing Programming Logic
- Module 2: Introducing Programming Logic
Moving beyond single, manually entered commands is the crucial leap from drawing to genuine programming. This module serves as the bridge, introducing students to the core concepts that enable them to write efficient, reusable, and dynamic code. By mastering repetition, variables, and procedures, students learn to manage complexity and build sophisticated programs from simple building blocks.
3.1. Efficiency Through Repetition
Computer programs excel at performing repetitive tasks. To draw a square, a student might initially write a long, repetitive sequence of commands:
fd 100 rt 90 fd 100 rt 90 fd 100 rt 90 fd 100 rt 90
This approach is inefficient and difficult to read. Logo provides a powerful command for handling such iteration: the repeat command. Its general form is:
repeat number [commands]
Using this structure, the square example can be rewritten far more efficiently:
repeat 4 [fd 100 rt 90]
This version is not only shorter but also more clearly communicates the program’s intent: to repeat the action of drawing a side and turning four times. It is also possible to place a repeat command inside another one, a technique known as nesting, which allows for the creation of highly complex patterns.
3.2. Storing Information with Variables
A variable is a named memory location that can hold a value. This allows programs to store and manipulate information. In Logo, variables are created with the make command and accessed using a colon.
- To assign a value, use the make command, which takes two arguments: the variable’s name (as a quoted word) and its value. For example: make “first_name “Amal.
- To access or retrieve the stored value, use a colon (:) before the variable name. For example: print :first_name.
Educator’s Note on Syntax: The standard syntax in MSW Logo for creating a variable is make “variable_name value. The double quote before the variable name is important. While some versions of Logo allow make variable_name value for simple string assignments (treating Amal as a string by default), this can cause confusion. It is best practice to teach the standard make “varname … form consistently for all variable types to prevent errors and build good habits.
Variables can hold different types of data, such as text (strings) or numbers.
String Variable Example: make “first_name “Amal print :first_name
Numeric Variable Example: make “val1 100 make “val2 200 print :val1 + :val2
3.3. Building Abstractions with Procedures
A procedure is a named sequence of commands that can be executed just like a built-in command. This powerful feature allows students to create their own custom commands, encapsulating a set of instructions to perform a specific task. This process of abstraction is a cornerstone of computer science.
The syntax for defining a procedure has three required parts:
- The keyword to, followed by the new procedure’s name (e.g., to square).
- The body of the procedure, which contains the sequence of commands to be executed.
- The keyword end to signal the end of the procedure definition.
To define a procedure for drawing a square, enter the following code into the Logo editor (opened by typing edall or clicking the Edall button):
to square
repeat 4 [fd 100 rt 90]
end
Once defined, the procedure can be run simply by typing its name (square) in the command line.
To make procedures more flexible and powerful, they can be designed to accept arguments. This allows the user to pass in a value that the procedure can use, making it adaptable to different situations.
To define a square procedure that accepts a size argument, use this code:
to square :n
repeat 4 [fd :n rt 90]
end
In this version, :n acts as a variable within the procedure. Now, the user can specify the size of the square when calling the command (e.g., square 50 or square 100), making the procedure far more reusable.
With these foundational logic tools, students can now progress to more advanced computational concepts like decision-making and recursion.