In this section, we will explore the various operators available in ALGOL and how to use them to form expressions. Understanding operators and expressions is fundamental to performing calculations, making decisions, and manipulating data in your programs.
- Arithmetic Operators
Arithmetic operators are used to perform basic mathematical operations. Here are the primary arithmetic operators in ALGOL:
Operator | Description | Example |
---|---|---|
+ |
Addition | A + B |
- |
Subtraction | A - B |
* |
Multiplication | A * B |
/ |
Division | A / B |
:= |
Assignment | A := B |
Example:
BEGIN INTEGER A, B, C; A := 10; B := 5; C := A + B; (* C is now 15 *) C := A - B; (* C is now 5 *) C := A * B; (* C is now 50 *) C := A / B; (* C is now 2 *) END
- Relational Operators
Relational operators are used to compare two values. The result of a relational operation is a Boolean value (TRUE
or FALSE
).
Operator | Description | Example |
---|---|---|
= |
Equal to | A = B |
≠ |
Not equal to | A ≠ B |
< |
Less than | A < B |
> |
Greater than | A > B |
≤ |
Less than or equal | A ≤ B |
≥ |
Greater than or equal | A ≥ B |
Example:
BEGIN INTEGER A, B; BOOLEAN Result; A := 10; B := 5; Result := A = B; (* Result is FALSE *) Result := A ≠ B; (* Result is TRUE *) Result := A < B; (* Result is FALSE *) Result := A > B; (* Result is TRUE *) Result := A ≤ B; (* Result is FALSE *) Result := A ≥ B; (* Result is TRUE *) END
- Logical Operators
Logical operators are used to combine multiple Boolean expressions.
Operator | Description | Example |
---|---|---|
AND |
Logical AND | A AND B |
OR |
Logical OR | A OR B |
NOT |
Logical NOT | NOT A |
Example:
BEGIN BOOLEAN A, B, Result; A := TRUE; B := FALSE; Result := A AND B; (* Result is FALSE *) Result := A OR B; (* Result is TRUE *) Result := NOT A; (* Result is FALSE *) END
- Assignment Operators
Assignment operators are used to assign values to variables. The basic assignment operator in ALGOL is :=
.
Example:
- Compound Expressions
Compound expressions combine multiple operations into a single expression. The order of operations follows standard mathematical precedence rules (parentheses, multiplication and division, addition and subtraction).
Example:
BEGIN INTEGER A, B, C, Result; A := 10; B := 5; C := 2; Result := (A + B) * C; (* Result is 30 *) Result := A + B * C; (* Result is 20 *) END
Practical Exercises
Exercise 1:
Write an ALGOL program to calculate the area of a rectangle. The program should prompt the user to enter the length and width of the rectangle and then display the area.
Solution:
BEGIN REAL Length, Width, Area; PRINT("Enter the length of the rectangle: "); READ(Length); PRINT("Enter the width of the rectangle: "); READ(Width); Area := Length * Width; PRINT("The area of the rectangle is: ", Area); END
Exercise 2:
Write an ALGOL program to determine if a number entered by the user is even or odd.
Solution:
BEGIN INTEGER Number; PRINT("Enter a number: "); READ(Number); IF Number MOD 2 = 0 THEN PRINT("The number is even.") ELSE PRINT("The number is odd."); END
Common Mistakes and Tips
- Mistake: Forgetting to use the assignment operator
:=
instead of the equality operator=
.- Tip: Remember that
:=
is used to assign values, while=
is used to compare values.
- Tip: Remember that
- Mistake: Misplacing parentheses in compound expressions.
- Tip: Use parentheses to clearly define the order of operations in complex expressions.
Conclusion
In this section, we covered the fundamental operators and expressions in ALGOL, including arithmetic, relational, logical, and assignment operators. We also explored how to combine these operators into compound expressions. Understanding these concepts is crucial for performing calculations and making decisions in your programs. In the next module, we will delve into control structures, which will allow you to control the flow of your programs based on conditions and loops.