Regular Operations

 

Algebraic Operations

 

The Addition

The addition is an operation used to add one number to another. The addition gives you to ability to add things of the same nature one to another, as many as necessary, until all items have been added. Sometimes, the items are added one group to another. The concept is still the same, except that this last example is faster. For example, if you have a group of eleven students and you add 5 students to the group, you can apply the same rule by adding one student at a time. But to perform this operation faster, you should work to add groups of items to one another. 

The addition is performed in mathematics using the + sign. The same sign is used in Object Pascal. To get the addition of two values, you add the first one to the other. After the addition of two values has been performed, you get a new value. This means if you add Value1 to Value2, you would write Value1 + Value2. The result is another value we could call Value3. You can also add more than two values, like a + b + c.

The order you use to add two or more values is not important. This means Value1 + Value2 is the same as Value2 + Value1. In the same way a + b + c is the same as a + c + b the same as b + a + c and the same as c + b + a. You can add values you know already, or you can use values the program gets from the user.

The simplest way to add values in Object Pascal is by performing a simple sum. Here is an example:

program Project1;

{$APPTYPE CONSOLE}

begin
	// Get the sum of two values
	Writeln('244 + 835 = ', 244 + 835);

	Write('Press any key to continue...');
	Readln;
end.

Here is the result:

244 + 835 = 1079
Press any key to continue...

You can also add some values already declared and initialized in your program. Here is an example:

program Project1;

{$APPTYPE CONSOLE}
var
	a, b: Integer;
begin
	a := 244;
	b := 835;

	// Get the sum of a and b
	Writeln(a, ' + ', b, ' = ', a + b);

	Write('Press any key to continue...');
	Readln;
end.

You can also get the values from the user as illustrated in the following program:

program Project1;

{$APPTYPE CONSOLE}
var
	a, b, c: Integer;
begin
	// Get the sum of a, b, and c.
	Write('Type the first value: ');
	Readln(a);
	Write('Type the second value: ');
	Readln(b);

	c := a + b;

	Writeln;
	Writeln(a, ' + ', b, ' = ', c);

	Write('Press any key to continue...');
	Readln;
end.

Here is an example of running the program:

Type the first value: 406
Type the second value: 1255

406 + 1255 = 1661
Press any key to continue...

 

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. 

The subtraction in Object Pascal is performed with the - symbol.

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. 

The multiplication is performed with the * sign which is typed with Shift + 8.

Just like the addition, the multiplication is associative: a * b * c = c * b * a.

When it comes to programming syntax, the rules we learned with the addition operation also apply to the multiplication.

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.

Object Pascal provides two type of divisions. A division performed on floating-point numbers uses the forward slash / symbol.

When performing the division, be aware of its many rules. Never divide by zero (0). Make sure that you know the relationship(s) between the numbers involved in the operation.

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.

Here is an example:

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.

The remainder operation is performed using the mod operator. Here is an example:

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.

The simplest technique of incrementing a value consists of adding 1 to it. After adding 1, the value or the variable is (permanently) modified and the variable would hold the new value. This is illustrated in the following example:

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 Post-Decrementing

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 floating-point number.

To cast a value from one type to another, you can use the following syntax:

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.

If you ask a program to add two numbers, for example 240 + 65, the program will execute the operation by adding 240 to 65. In other words, it would read 240, then +, then 65, and evaluate the result. This is considered as operating from left to right: 240 -> + -> 65. This process is referred to as the direction of the operation.

As you will regularly combine operators on your various calculations, each operation is known for how much it "weighs" as compared to other operators. This is known as its precedence. This means that when a certain operator is combined with another, such as a + b * c, or x / y - z, some operators would execute before others, almost regardless of how you write the operation. That's why an operator could be categorized by its level of precedence. Object Pascal categorizes precedence as follows:

Operators Precedence
@, not First (highest)
*, /, div, mod, and, shl, shr, as Second
+, –, or, xor Third
=, <>, <, >, <=, >=, in, is fourth (lowest)

 


Previous Copyright © 2004 FunctionX Next