Regular Operations 


The Subtraction 
The subtraction operation is used to take out or subtract a value from another value. It is essentially the opposite of the addition. 
program Subtract; uses SysUtils; {$APPTYPE CONSOLE} // Values used in this program var Value1, Value2, Value3: Integer; begin // Get the values from the user Write('Type the first number: '); Readln(Value1); Write('Type another number: '); Readln(Value2); // Subtract the first value from the second Value3 := Value1  Value2; Writeln; Writeln(Value1, '  ', Value2, ' = ', Value3); Readln; end.
The Multiplication 
The multiplication allows adding one value to itself a certain number of times, set by a second value. As an example, instead of adding a value to itself in this manner: a + a + a + a, since the variable a is repeated over and over again, you could simply find out how many times a is added to itself, then multiply a by that number which, is this case, is 4. This would mean adding a to itself 4 times, and you would get the same result. 
program Multiply1; uses SysUtils; {$APPTYPE CONSOLE} var a, b: Single; begin // Multiple of a and b. Writeln('  Type two values '); Write('First: '); Readln(a); Write('Second: '); Readln(b); Writeln; Writeln(a:0:2, ' * ', b:0:2, ' = ', (a * b):0:2); Write('Press any key to continue...'); Readln; end.
The Real Division 
Dividing an item means cutting it in pieces or fractions of a set value. For example, when you cut an apple in the middle, you are dividing it in 2 pieces. If you cut each one of the resulting pieces, you will get 4 pieces or fractions. This is considered that you have divided the apple in 4 parts. Therefore, the division is used to get the fraction of one number in terms of another. 
program Division1; uses SysUtils; {$APPTYPE CONSOLE} var a: Single; begin // Get a number from the user. Write('Type a number: '); Readln(a); Writeln; Writeln('Half of ', a:2:2, ' is ', (a / 2):2:2); Write('Press any key to continue...'); Readln; end.
Execute the program to test it. Here is an example:
Type a number: 258.64 Half of 258.64 is 129.32 Press any key to continue...
Integer Division 
Object Pascal provides another operator to perform a division. This one applies only to integral values and uses the div operator. 
program Project1; {$APPTYPE CONSOLE} var // Number of students in Class1, Class2, Class3, Class4, Class5: Integer; TotalStudents, AvgInClass: Integer; begin Class1 := 35; Class2 := 28; Class3 := 42; Class4 := 38; Class5 := 44; TotalStudents := Class1 + Class2 + Class3 + Class4 + Class5; AvgInClass := TotalStudents div 5; Writeln('Average number of students in a class: ', AvgInClass); Write('Press any key to continue...'); Readln; end.
This would produce:
Average number of students in a class: 37 Press any key to continue...
The Remainder 
The division program above will give you a result of a number with decimal values if you type an odd number (like 147), which is fine in some circumstances. Sometimes you will want to get the value remaining after a division renders a natural result. 
program Project1; {$APPTYPE CONSOLE} var Players: Integer; begin Players := 16; // When the game starts, how many players will wait?. Writeln('Out of ', Players, ' players, ', 26 mod 11, ' players will have to wait when the ', ' football match starts.'); Write('Press any key to continue...'); Readln; end.
This would produce:
Out of 16 players, 4 players will have to wait when the football match starts. Press any key to continue...
Incrementing a Number 
We are used to counting numbers such as 1, 2, 3, 4, etc. In reality, when counting such numbers, we are simply adding 1 to a number in order to get the next number in the range. Object Pascal provides a technique of transparently counting such numbers. 
program Project1; {$APPTYPE CONSOLE} var Value: Integer; begin Value := 12; Writeln('Value = ', Value); Value := Value + 1; Writeln('Value = ', Value); Write('Press any key to continue...'); Readln; end.
This would produce:
Value = 12 Value = 13
Decrementing – Pre and PostDecrementing 
When counting numbers backward, such as 8, 7, 6, 5, etc, we are in fact subtracting 1 from a value in order to get the lesser value. This operation is referred to as decrementing a variable. This operation works as if a variable called Value has its value diminished by 1, as in Value = Value – 1: 
program Project1; {$APPTYPE CONSOLE} var Value: Integer; begin Value := 15; Writeln('Value = ', Value); Value := Value  1; Writeln('Value = ', Value); Write('Press any key to continue...'); Readln; end.
This would produce:
Value = 15 Value = 14 Press any key to continue...
Variable Casting 
Introduction 
A compiler has the duty of rendering good results, but sometimes it cannot. It may not be able to figure out what is going on in your program. This means that, sometimes you have to be explicit. When performing operations on variables of different types, you need to tell the compiler what form of result you are expecting. For example, you can ask the compiler to convert a variable or result from one type to another, otherwise the compiler might just "make up its own mind". Most of the time you will not like it. Value casting consists of converting the type of data that the value is holding into another type. 
Value Casting 
In order to cast a value into another, the new value must be able to fit into the memory space of the old one. This means that an integer cannot be cast with a floatingpoint number. DataType(Expression) Here is an example: 
program Project1; {$APPTYPE CONSOLE} var Value: Integer; begin Value := 104; Writeln(Value, ' cast to a char is ', Char(Value)); Write('Press any key to continue...'); Readln; end.
This would produce:
104 cast to char is h Press any key to continue...
Operator Precedence and Direction 
When combining operations in Object Pascal, there are two aspects involved: an operator's precedence and its direction. 



Previous  Copyright © 2004 FunctionX  Next 
