1 Logical operators include: and, or, not, xor
These operators take boolean values as operands and also return boolean results;
When both operands are integers, and/or/not/xor become bitwise operators and produce integer results.
2 Relational (comparison) operators: =, <>, >, <, <=, >=.
If you are familiar with C-family languages, pay close attention to these differences:
Pascal uses a single equals sign for equality checks: A = B (C languages use A == B);
Pascal’s dedicated assignment operator is :=.
Logical / bitwise operators have higher precedence than relational operators.
Parentheses must wrap certain expressions to control evaluation order; otherwise logic errors or compilation failures will occur.
Incorrect example (compiler error)
var
A, B: Integer;
begin
if A = 0 and B <> 0 then ... // Wrong syntaxCode language: JavaScript (javascript)
Reason for compilation failure:
Compiler precedence rules execute the middle bitwise AND 0 and B first (resulting in an integer);
Then compute A = (0 and B), yielding a boolean value;
The program then attempts to compute between a boolean and integer 0, triggering a type mismatch error.
Correct syntax
var
A, B: Integer;
begin
if (A = 0) and (B <> 0) then ...Code language: HTML, XML (xml)
Short-circuit evaluation rules
Take the expression if MyFunction(X) and MyOtherFunction(Y) then... as an example, rules are as follows:
andoperation: Evaluate left-hand sideMyFunction(X)first- If the left side returns
false, the entire expression is guaranteedfalse, and the right-hand functionMyOtherFunction(Y)will not execute at all.
- If the left side returns
oroperation works similarly:- If the left side already returns
true, the overall result is true, and right-side code skips execution.
- If the left side already returns
This feature is extremely useful for null pointer checks:
if (A <> nil) and A.IsValid then...Code language: HTML, XML (xml)
The code will not crash even if A is the null pointer nil. Keyword nil represents null pointer, numerically equivalent to 0, corresponding to null in other languages.
Operator Classification
Relational Operators (return Boolean)
| Pascal | Meaning | C Equivalent |
|---|---|---|
= | Equal to | == |
<> | Not equal to | != |
> | Greater than | > |
< | Less than | < |
>= | Greater than or equal to | >= |
<= | Less than or equal to | <= |
Logical / Bitwise Operators
not: Logical NOT / Bitwise NOT and: Logical AND / Bitwise AND or: Logical OR / Bitwise OR xor: Logical XOR / Bitwise XOR
not (NOT)
| a | not a |
|---|---|
| true | false |
| false | true |
and (AND, true only when both operands true)
| a | b | a and b |
|---|---|---|
| true | true | true |
| true | false | false |
| false | true | false |
| false | false | false |
or (OR, true if at least one operand true)
| a | b | a or b |
|---|---|---|
| true | true | true |
| true | false | true |
| false | true | true |
| false | false | false |
xor (XOR, true when operands differ)
| a | b | a xor b |
|---|---|---|
| true | true | false |
| true | false | true |
| false | true | true |
| false | false | false |
Examples
Leap Year Judgment Rules
- Divisible by 4, but not divisible by 100 → Leap year
- Divisible by 400 → Leap year All others are common years
{$ifdef FPC} {$mode objfpc} {$H+} {$endif}
{$codepage utf8}
program LeapYearCheck;
// Function to judge leap year, input year, return Boolean
function IsLeapYear(year: Integer): Boolean;
begin
Result := ((year mod 4 = 0) and (year mod 100 <> 0)) or (year mod 400 = 0);
end;
var
y: Integer;
begin
Write('Enter year: ');
ReadLn(y);
if IsLeapYear(y) then
Writeln(y, ' is a leap year')
else
Writeln(y, ' is a common year');
ReadLn;
end.Code language: JavaScript (javascript)

modremainder operator,year mod 4 = 0means divisible by 4- Expressions must use parentheses:
andhas higher precedence than comparison operators - Short-circuit feature: If the
orprior condition of divisible by 400 is satisfied, the 4/100 conditions will not be evaluated
Logical, Relational and Bitwise Operators