Understanding the basic syntax rules of Pascal is crucial, as the compiler is very strict. Pascal is a strongly typed language, meaning every variable must be declared with a specific data type, and that type cannot change.
1. Pascal Syntax Rules
| Rule | Description | Example |
| Semicolons | Used as a statement separator, not a terminator. They separate one command from the next. The last statement before an end or else often omits it. | Writeln('A'); Writeln('B') |
| Case Insensitivity | Pascal keywords and identifiers are case-insensitive. VAR, var, and Var are all treated the same. | myVariable := 10; MyVariable := 20; (Same variable) |
| Comments | Used to add notes for developers. The compiler ignores them. | // Single-line comment |
| Block Comments | Used for multi-line notes. | (* Multi-line comment *) or { Another multi-line comment } |
| Assignment | The assignment operator is a colon followed by an equals sign. | MyAge := 30; |
| Comparison | The equality operator uses a single equals sign. | if A = B then... |
2. Variable Declaration
In Pascal, all variables must be declared in a dedicated var section before the begin of the program or procedure.
program Declarations;
var
// Syntax: VariableName : DataType;
UserAge : Integer;
UserName : String;
IsActive : Boolean;
begin
// ... code goes here ...
end.
3. Fundamental Data Types
Pascal has distinct categories for storing whole numbers, decimals, characters, and logical values.
A. Ordinal Types (Whole Numbers)
Ordinal types have a definite, ordered set of values (e.g., you can say what the “next” value is).
| Type | Purpose | Typical Range (32-bit) | Memory Size |
Integer | Standard 32-bit signed whole number. | -2,147,483,648 to 2,147,483,647 | 4 bytes |
SmallInt | Smaller 16-bit signed whole number. | -32,768 to 32,767 | 2 bytes |
Byte | Smallest 8-bit unsigned whole number. | 0 to 255 | 1 byte |
B. Real Types (Decimal Numbers)
Real types store fractional numbers. They do not have defined successors or predecessors, so they are not ordinal types.
| Type | Purpose | Key Feature |
Real | General-purpose floating-point number. (Historically 6-byte, but often maps to Double in modern FPC/Delphi.) | Standard precision and range. |
Double | 64-bit floating-point number. | High precision, standard for general calculation. |
Extended | 80-bit floating-point number. | Highest precision, often used for critical financial calculations. |
C. Other Simple Types
| Type | Purpose | Storage | Example |
Boolean | Logical value. Can only be True or False. | 1 byte | IsFound := True; |
Char | A single character, enclosed in single quotes. | 1-2 bytes (ASCII/Unicode) | Grade := 'A'; |
String | A sequence of characters. Length is dynamic. | Varies | FirstName := 'Alice'; |
4. Assignments and Operations
You assign values to variables and perform calculations using operators.
A. Example Assignment
var
A, B: Integer;
Sum: Integer;
IsHigh: Boolean;
begin
A := 10; // Assigns 10 to A
B := 5; // Assigns 5 to B
Sum := A + B; // Arithmetic: Sum is 15
IsHigh := (A > 15); // Comparison: IsHigh is False
end.
B. Arithmetic Operators
| Operator | Purpose | Example | Result |
+ | Addition | 5 + 2 | 7 |
- | Subtraction | 5 - 2 | 3 |
* | Multiplication | 5 * 2 | 10 |
/ | Real Division (always returns a floating-point number) | 5 / 2 | 2.5 |
div | Integer Division (returns whole quotient) | 5 div 2 | 2 |
mod | Modulus (remainder of integer division) | 5 mod 2 | 1 |
var
Total: Real;
Quotient: Integer;
begin
Total := 10 / 3; // Total is 3.333... (Real division)
Quotient := 10 div 3; // Quotient is 3 (Integer division)
end.
