4.0 Programming Constructs
4.0 Programming Constructs
Logo extends beyond simple drawing commands to include fundamental programming constructs that are essential for creating dynamic and complex programs. These capabilities allow for state management through variables, data manipulation via arithmetic operators, and text processing using string functions. Mastering these constructs enables developers to build programs that can store information, perform calculations, and adapt their behavior based on internal data. This foundation in data manipulation is a prerequisite for understanding how to control the flow of program execution.
4.1 Variables and Data Management
In Logo, a variable is a named memory location used to store a value, which can be either a number or a string.
- Creation and Assignment: Variables are created and assigned a value using the make command. The variable name must be preceded by a double quote (“).
- Example (Numeric): make “my_var 100
- Example (String): make “first_name “Amal”
- Accessing Values: To access or use the value stored in a variable, its name must be preceded by a colon (:).
- Example: fd :my_var
- Example: print :first_name
4.2 Arithmetic and Mathematical Operations
Logo supports standard arithmetic operators that produce a result. This result must be used by another command, such as print, to avoid an error.
- Standard Operators: + (addition), – (subtraction), * (multiplication), / (division).
- Mathematical Functions: The following functions are also available for more complex calculations.
| Command | Function |
| sqrt [arg] | Returns the square root of its non-negative argument. |
| power [a] [b] | Returns a to the power of b. |
| ln [arg] | Returns the natural logarithm of its argument. |
| exp [arg] | Computes e to the power of its argument. |
| log10 [arg] | Returns the base-10 logarithm of its argument. |
- Operator Precedence: In Logo, infix operators like * have a higher precedence when parsing command arguments. This affects the order of evaluation in complex expressions.
- print 60 * sqrt 2 first calculates the square root of 2, and then multiplies the result by 60.
- print sqrt 2 * 60 behaves differently. Because * has higher precedence, the expression 2 * 60 is evaluated first to 120. This result is then passed as the single argument to the sqrt command, which calculates the square root of 120.
4.3 String Manipulation
A string in Logo is a sequence of alpha-numeric characters. The language provides several commands for processing and deconstructing strings. The following procedure demonstrates how to combine variable assignment, a while loop, and string functions to calculate the length of a string.
to stringlength :s
make “inputstring :s
make “count 0
while [not emptyp :s] [
make “count :count + 1
make “s butfirst :s
]
print (sentence :inputstring “has :count “letters)
end
This procedure utilizes three key string-processing commands:
- emptyp: This function tests if a string is empty. It returns true if the string has no characters and false otherwise. In the while loop, not emptyp :s continues the loop as long as the string :s contains characters.
- first: Returns the first character of a string. (While not used in the final version of the example procedure, it is a related core function.)
- butfirst: Returns the entire string except for its first character. In each iteration of the loop, make “s butfirst :s effectively shortens the string by one character from the front, ensuring the loop eventually terminates.