1. Declaring Variables


var                               // This starts a section of variables
LineTotal :      Integer;  // This defines an Integer variable called LineTotal
First,Second : String;     // This defines two variables to hold strings of text

Assigning Values to Variables


Variables are simply a name for a block of memory cells in main memory. If a value is assigned to a variable, that value must be of the same type as the variable, and will be stored in the memory address designated by the variable name. The assignment statement is the semicolon-equal :=.

  • Variables must be declared at the beginning of the program, a procedure, or a function  
  • Variables must be initialized before they can be used.  
  • Variables can be reused as often as necessary. Their old value is simply overwritten by a new assignment.  

Example:

Var
i : Integer:     { variable name is i, type is integer)
Begin
i := 10;        { valid integer number assigned to variable i }
End.

2. Variable Types

Numeric Data Types

Type Storage size Range
Byte 1 0 to 255
ShortInt 1 -127 to 127
Word 2 0 to 65,535
SmallInt 2 -32,768 to 32,767
LongWord 4 0 to 4,294,967,295
Cardinal 4* 0 to 4,294,967,295
LongInt 4 -2,147,483,648 to 2,147,483,647
Integer 4* -2,147,483,648 to 2,147,483,647
Int64 8 -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
Single 4 7  significant digits, exponent   -38 to +38
Currency 8 50+ significant digits, fixed 4 decimal places
Double 8 15  significant digits, exponent  -308 to +308
Extended 10 19  significant digits, exponent -4932 to +4932

Assigning to and from number variables


Number variables can be assigned from other numeric variables, and expressions:

var
Age       : Byte;       // Smallest positive integer type
Books    : SmallInt;   // Bigger signed integer
Salary         : Currency;   // Decimal used to hold financial amounts
Expenses    : Currency;
TakeHome : Currency;
begin
Expenses      := 12345.67;  // Assign from a literal constant
TakeHome   := Salary;    // Assign from another variable
TakeHome   := TakeHome - Expenses;  // Assign from an expression
end;

Numerical operators


Number calculations, or expressions, have a number of primitive operators available:

+       Add one number to another
-       Subtract one number from another
*       Multiply two numbers
/       Divide one decimal number by another
div     Divide one integer number by another
mod     Remainder from dividing one integer by another

When using these multiple operators in one expression, you should use round brackets to wrap around sub-expressions to ensure that the result is obtained. This is illustrated in the examples below:

var
myInt : Integer;  // Define integer and decimal variables
myDec : Single;
begin
myInt := 20;           // myInt is now 20
myInt := myInt + 10;   // myInt is now 30
myInt := myInt - 5;    // myInt is now 25
myInt := myInt * 4;    // myInt is now 100
myInt := 14 div 3;     // myInt is now 4   (14 / 3 = 4 remainder 2)
myInt := 14 mod 3;     // myInt is now 2   (14 / 3 = 4 remainder 2)
myInt := 12 * 3 - 4;   // myInt is now 32  (* comes before -)
myInt := 12 * (3 - 4); // myInt is now -12 (brackets come before *)
myDec := 2.222 / 2.0;  // myDec is now 1.111
end;

Character Types

var
Str1 : Char;            // Holds a single character, small alphabet
Str2 : WideChar;        // Holds a single character, International alphabet
Str3 : AnsiChar;        // Holds a single character, small alphabet
Str4 : ShortString;     // Holds a string of up to 255 Char's
Str5 : String;          // Holds strings of Char's of any size desired
Str6 : AnsiString;      // Holds strings of AnsiChar's any size desired
Str7 : WideString;      // Holds strings of WideChar's of any size desired

Some simple text variable usage examples are given below:

+       Concatenates two strings together
=       Compares for string equality
<       Is one string lower in sequence than another
<=     Is one string lower or equal in sequence with another
>       Is one string greater in sequence than another
>=    Is one string greater or equal in sequence with another
<>    Compares for string inequality

Variants


The Variant data type provides a flexible general purpose data type.

It can hold anything but structured data and pointers.

Variants are useful in very specific circumstances, where data types and their content are determined at run time rather than at compile time.

Example:

var
myVar : Variant;

Date Variables


TDateTime

Description

The TDateTime type holds a date and time value.

It is stored as a Double variable, with the date as the integral part, and time as fractional part. The date is stored as the number of days since 30 Dec 1899. Quite why it is not 31 Dec is not clear. 01 Jan 1900 has a days value of 2.

Because TDateTime is actually a double, you can perform calculations on it as if it were a number. This is useful for calculations such as the difference between two dates.

Note:
No local time information is held with TDateTime - just the day and time values.


Example:

Finding the difference between two dates

var
day1, day2 : TDateTime;
diff : Double;
begin
day1 := StrToDate('12/06/2002');
day2 := StrToDate('12/07/2002');
diff := day2 - day1;
Result:='day2 - day1 = '+FloatToStr(diff)+' days';
end;

day2 - day1 = 30 days

Logical data types


These are used in conjunction with programming logic. They are very simple:

Var
Log1 : Boolean;     // Can be 'True' or 'False'

Boolean variables are a form of enumerated type. This means that they can hold one of a fixed number of values, designated by name. Here, the values can be True or False.

Next Logical Operations

We have 273 guests and 2 members online