3.0 Core Programming Constructs
3.0 Core Programming Constructs
Logo implements fundamental programming concepts that enable the creation of complex and dynamic programs.
3.1 Variables
A variable is a named memory location that can store a value.
- Naming: Variable names can contain letters (case-insensitive), digits, and underscores.
- Creation: The make command assigns a value to a variable. The variable name is preceded by a double quote (e.g., make “first_name Amal).
- Access: To use a variable’s value in a computation, its name is preceded by a colon (e.g., print :first_name).
3.2 Arithmetic Operations
Logo supports standard arithmetic and several mathematical functions. A result from an operation must be acted upon (e.g., by the print command) to avoid an error.
- Standard Operators: + (addition), – (subtraction), * (multiplication), / (division).
- Functions:
- sqrt: Returns the square root of its argument.
- power: Takes two arguments (a and b) and returns a to the power of b.
- ln: Returns the natural logarithm.
- exp: Computes e (2.718281828) to the power of its argument.
- log10: Returns the base-10 logarithm.
- Operator Precedence: The order of operations is important. The expression print 60 * sqrt 2 multiplies 60 by the square root of 2, whereas print sqrt 2 * 60 calculates the square root of 120, because the * operator has precedence over sqrt.
3.3 Repetition and Nesting
The repeat command provides a simple way to perform iteration.
- Syntax: repeat number [commands]
- Functionality: Executes the sequence of commands within the square brackets a specified number of times. For example, repeat 4 [fd 100 rt 90] draws a square.
- Nesting: A repeat loop can be placed inside another repeat loop to create more complex patterns.
3.4 Decision-Making
Logo programs can alter their flow of execution based on specific conditions.
- while Loop: Executes a block of code as long as a condition remains true. The syntax is while [condition] [commands].
- if Statement: Executes a block of code only if a given condition is true. This allows for conditional logic within a program.