The Equality Operator
|
Object Pascal provides an operator highly used
to perform comparisons but it is also used to assign a value to a type.
The operator is =. This operator is primarily used to create a type of
data as we used it when introducing enumerators. We will also use it
with classes, etc.
In the previous lesson, we saw that a constant must be initialized in order to be used. This is taken carer of using the equality operator. Here is an example:
In the same way, you can create and initialize
as many constants as your program needs. Once such a constant has been
initialized, it can be accessed by its name. For example, its name can
be provided to the Write or the Writeln procedures to display on a
console window:
|
Parentheses
|
Like most computer languages, Object Pascal
uses parentheses to isolate a group of items that must be considered as
belonging to one entity. For example, as we will learn soon, parentheses
allow a function or a procedure to delimit the list of its arguments.
Parentheses can also be used to isolate an operation or an expression with regard to another operation or expression. For example, when studying the algebraic operations, we will see that the subtraction is not associative and can lead to unpredictable results. In the same way, if your operation involves various operators such as addition(s) and subtraction(s), you can use parentheses to tell the compiler how to proceed with the operations, that is, what operation should (must) be performed first. Consider the following algebraic operation: 154 - 12 + 8 The question here is to know whether you want to subtract the addition of 12 and 8 from 154 or you want to add the difference between 154 and 12 to 8. Using parentheses, you can communicate your intentions to the compiler. This is illustrated in the following program: |
program Project1; {$APPTYPE CONSOLE} begin Write('(154 - 12) + 8 = '); Writeln((154 - 12) + 8); Write(' 154 - (12 + 8) = '); Writeln(154 - (12 + 8)); Writeln('Press any key to continue...'); Readln; end. |
This would produce:
|
(154 - 12) + 8 = 150 154 - (12 + 8) = 134 Press any key to continue... |
As you can see, using the parentheses controls
how the whole operation would proceeds This difference can be even more
accentuated if your operation includes 3 or more operators and 4 or
more operands.
|
The Comma
|
A comma is used in Object Pascal to separate a
list of items that come as a group. In the previous lesson, we used
commas when creating enumerators and other types of variables. Another
way you can use a comma is to separate items to display in a Write or a
Writeln procedure. Here is an example:
program Project1; {$APPTYPE CONSOLE} var Country: string; NumberOfPages: Integer; begin Country := 'Sri Lanka'; NumberOfPages := 812; Writeln('Country: ', Country); Writeln('Number Of Pages: ', NumberOfPages); Write('Press any key to continue...'); Readln; end.
This would produce:
Country: Sri Lanka Number Of Pages: 812 Press any key to continue... |
Colons
|
In the previous lesson, we used the colons
when declaring variables. A colon can also be used to format the display
of a floating-point number. To do this, type the number or variable
followed by a colon. Then, on the right side of the colon, you can type
an integer number. Here is an example:
program Colons; uses SysUtils; {$APPTYPE CONSOLE} var a: Single; begin a := 126.06; Write(a:2); Readln; end.
This would produce:
1.2E+0002
When writing a:2, the compiler is asked to
provide a minimum space to display the value of a. If you want to
control the number of decimal places when displaying the number, add
another colon followed by the number of decimal values to the right of
the decimal separator. Here is an example:
program Colons; uses SysUtils; {$APPTYPE CONSOLE} var a: Single; begin a := 126.0684; Write(a:4:2); Readln; end.
This would produce:
126.07 |
Square Brackets
|
Square brackets are mostly used to control the
dimension or index of an array. We will learn how to use them when we
study arrays.
|
Unary Operators
|
Unary Operators: The Positive Operator +
|
Algebra uses a type of ruler to classify
numbers. This ruler has a middle position of zero. The numbers on the
left side of the 0 are considered negative while the numbers on the
right side of the 0 are considered
positive:
|
-∞ | -6 | -5 | -4 | -3 | -2 | -1 | 1 | 2 | 3 | 4 | 5 | 6 | +∞ | |||
0 | ||||||||||||||||
-∞ | -6 | -5 | -4 | -3 | -2 | -1 | 1 | 2 | 3 | 4 | 5 | 6 | +∞ |
A value on the right side of 0 is considered
positive. To express that a number is positive, you can write a + sign
on its left. Examples are +4, +228, +90335. In this case the + symbol is
called a unary operator because it acts on only one operand.
The positive unary operator, when used, must be positioned on the left side of its operand. As a mathematical convention, when a value is positive, you do not need to express it with the + operator. Just writing the number without any symbol signifies that the number is positive. Therefore, the numbers +4, +228, and +90335 can be, and are better, expressed as 4, 228, 90335. Because the value does not display a sign, it is referred as unsigned as we learned in the previous lesson. |
Unary Operators: The Negative Operator -
|
As you can see on the above ruler, in order to
express any number on the left side of 0, it must be appended with a
sign, namely the - symbol. Examples are -12, -448, -32706. A value
accompanied by - is referred to as negative.
The negative operator, –, must be typed on the left side of the number it is used to negate.
Remember that if a number does not have a
sign, it is considered positive. Therefore, whenever a number is
negative, it must have a - sign. In the same way, if you want to change a
value from positive to negative, you can just add a - sign to its left.
Here is an example that uses two variables. One has a positive value while the other has a negative value:
Here is an example that uses two variables. One has a positive value while the other has a negative value:
program Project1; {$APPTYPE CONSOLE} var Number1: Integer; Number2: Integer; begin Number1 := 802; Number2 := -62; Writeln('The value of the first number is: ', Number1); Writeln('The value of the second number is: ', Number2); Write('Press any key to continue...'); Readln; end.
This would produce:
The value of the first number is: 802 The value of the second number is: -62 Press any key to continue...
No comments:
Post a Comment