1. Program Structure and Flow
Object Pascal is a highly structured language. Code execution typically flows from the entry point (.dpr file) into the main units (.pas). Within units, code execution is governed by begin and end blocks.
- Execution Block: All executable code must be contained within a
beginandend;pair. - Case Insensitivity: Object Pascal is largely case-insensitive for keywords and identifiers (e.g.,
Begin,BEGIN, andbeginare all treated the same, but it is best practice to use standard casing). - Statement Termination: Unlike C-style languages that use the semicolon (
;) to end a statement, Pascal uses the semicolon as a statement separator. It is placed between statements, but not before anendorelsekeyword.
procedure TForm1.Button1Click(Sender: TObject);
begin
ShowMessage('Statement 1'); // Statement 1 followed by semicolon
ShowMessage('Statement 2') // Statement 2, no semicolon before end
end;
2. Variables
Variables are memory locations used to store data. In Object Pascal, variables must be declared before they can be used, and they are defined within a dedicated var block.
Declaration Syntax
The syntax for declaration is: VariableName: DataType;
procedure TForm1.FormCreate(Sender: TObject);
var
// Variables declared within a procedure are local to that procedure
UserName: String;
UserAge: Integer;
begin
UserName := 'Alice'; // Assignment operator is :=
UserAge := 30;
ShowMessage('Hello, ' + UserName);
end;
3. Basic Data Types
Object Pascal is a strongly-typed language, meaning a variable can only hold values that match its declared type.
A. Integer Types (Whole Numbers)
Used for counting, indices, and sizes. They are optimized for speed.
| Type | Range | Description |
Integer | Standard 32-bit signed integer. | Best for general-purpose use. |
Cardinal | Standard 32-bit unsigned integer. | Used for non-negative values like counts. |
Int64 | 64-bit signed integer. | Used for very large numbers. |
B. Real Types (Floating Point)
Used for numbers with fractional parts.
| Type | Precision | Description |
Extended | 18-20 decimal digits. | Highest precision available (80-bit). |
Double | 15-17 decimal digits. | Standard type (64-bit), good for most tasks. |
Single | 7-8 decimal digits. | Lower precision (32-bit), used where memory is critical. |
C. String Types (Text)
Delphi uses modern Unicode strings by default.
| Type | Description |
String | Standard type, an alias for UnicodeString in modern Delphi. Can hold virtually any amount of text. |
AnsiString | Older type for single-byte characters. Used for backward compatibility or interacting with older APIs. |
Char | A single Unicode character. |
D. Boolean Type
Used for logical testing and decision making.
| Type | Description |
Boolean | Can only hold the values True or False. |
4. Constants
Constants are identifiers whose values are fixed at compile time and cannot be changed during program execution.
- Syntax: Defined within a dedicated
constblock. - Benefit: Improves readability and ensures consistency (e.g., a fixed tax rate used throughout an application).
procedure TForm1.FormCreate(Sender: TObject);
const
// Constants must be initialized immediately
TaxRate = 0.05;
AppTitle = 'Inventory Manager';
MaxUsers: Integer = 100; // Type is optional for simple types
begin
ShowMessage('Welcome to ' + AppTitle);
end;
5. Type Casting and Conversion
Since Object Pascal is strongly typed, you must explicitly convert data between different types.
- Type Casting (Hard Cast): Used when you are certain the conversion is safe (common with object pointers or low-level memory). Syntax:
NewType(Expression) - Conversion Functions (Soft Cast): Used for converting between strings and numbers (safer).
var
S: String;
I: Integer;
R: Double;
begin
I := 123;
R := 45.67;
// Numerical to String Conversion
S := IntToStr(I); // S is '123'
S := FloatToStr(R); // S is '45.67'
// String to Numerical Conversion
I := StrToInt('10'); // I is 10
R := StrToFloat('9.99'); // R is 9.99
// Hard Cast (Example: converting an integer to a byte type)
var B: Byte;
B := Byte(I); // Assuming I is a value between 0 and 255
end;
