Pascal Syntax at a Glance
Every Pascal program follows a fixed skeleton: an optional program NameHere; header, an optional uses clause naming library units, then the declaration sections in order — const for named constants, type for custom type definitions, var for variable declarations — followed by any procedure and function definitions, and finally the single main begin..end. block terminated with a period. This ordering is not stylistic preference but a compiler requirement in standard Pascal: you cannot declare a variable of a type before that type itself is declared, and you cannot call a procedure defined further down the file without a forward declaration, which is why the skeleton always flows from broad declarations down to executable behavior.
Cricket analogy: The fixed program skeleton is like a Test match day's fixed structure — toss, then first innings, then subsequent innings in order — you cannot play the fourth innings before the first has happened.
Core Data Types and Operators
Pascal's built-in ordinal and simple types are Integer (typically -2147483648..2147483647 on modern 32-bit-range compilers), Real or Double for floating point, Char for a single character, Boolean for True/False, and String (AnsiString in modern Free Pascal/Delphi) for text; div and mod are the integer division and remainder operators (17 div 5 = 3, 17 mod 5 = 2), distinct from the / operator which always produces a Real result even for two integer operands. Relational operators (=, <>, <, >, <=, >=) compare values and produce a Boolean, while and, or, and not combine Boolean expressions — Standard Pascal evaluates and/or without short-circuiting by default, though Delphi and Free Pascal support {$B-} short-circuit boolean evaluation (the default) versus {$B+} full evaluation, which matters when one side of a boolean expression has a side effect or could fail.
Cricket analogy: div and mod splitting 17 into quotient 3 and remainder 2 is like distributing 17 overs among 5 bowlers as 3 full overs each with 2 overs left over for a sixth partial spell.
Control Structures Cheat Sheet
if..then..else handles branching, with the else clause always binding to the nearest preceding if (the classic 'dangling else' rule), and a semicolon placed directly before an else is a common compile error because it terminates the if statement early. case..of dispatches on an ordinal value with optional ranges (case Grade of 90..100: ...) and an optional else/otherwise clause for unmatched values. for..to/downto..do iterates a control variable across a fixed range in either direction, while..do checks its condition before each iteration (zero or more executions), and repeat..until checks its condition after the loop body (one or more executions), which is the single most commonly confused pair of loop constructs for newcomers moving from C-family languages.
Cricket analogy: The dangling-else rule binding to the nearest if is like an on-field appeal automatically being addressed to the nearest umpire, not an umpire from a different match entirely.
program QuickReferenceDemo;
const
PassMark = 40;
type
TGrade = 0..100;
var
Score: TGrade;
i, Total: Integer;
begin
Score := 85;
{ if..then..else }
if Score >= PassMark then
Writeln('Pass')
else
Writeln('Fail');
{ case..of with ranges }
case Score of
90..100: Writeln('Grade: A');
80..89: Writeln('Grade: B');
70..79: Writeln('Grade: C');
else
Writeln('Grade: below C');
end;
{ for..to..do }
Total := 0;
for i := 1 to 10 do
Total := Total + i;
Writeln('Sum 1..10 = ', Total); { 55 }
{ while..do : zero or more iterations }
i := 10;
while i > 7 do
begin
Writeln('while i = ', i);
Dec(i);
end;
{ repeat..until : one or more iterations }
i := 0;
repeat
Writeln('repeat i = ', i);
Inc(i);
until i >= 3;
Writeln('17 div 5 = ', 17 div 5, ', 17 mod 5 = ', 17 mod 5);
end.Quick rule of thumb: while..do is a pre-check loop that may run zero times, and repeat..until is a post-check loop that always runs at least once. If a task must happen even once before any validation (like prompting a user), reach for repeat..until; otherwise, default to while..do.
A semicolon placed directly before else — as in if X > 0 then Writeln('positive'); else Writeln('non-positive'); — is a compile error in Pascal, because the semicolon terminates the if statement before the compiler ever sees the else. Only place a semicolon after the final statement of the entire if..then..else construct.
- Program structure order is fixed: program header, uses, const, type, var, routines, then the main begin..end. block.
- Core simple types are Integer, Real/Double, Char, Boolean, and String, each with distinct operators and behaviors.
- div and mod perform integer division and remainder; / always yields a Real result.
- case..of supports value ranges and an else/otherwise clause for unmatched cases.
- while..do checks before the loop body (zero or more runs); repeat..until checks after (one or more runs).
- A semicolon directly before else is a classic compile error because it prematurely ends the if statement.
- Relational operators return Boolean, and and/or/not combine Boolean expressions, with short-circuit evaluation as the Free Pascal/Delphi default.
Practice what you learned
1. What is the correct fixed order of sections at the top of a standard Pascal program?
2. What does 17 div 5 evaluate to in Pascal?
3. How many times does a while..do loop body execute if its condition is False from the start?
4. Why does if X > 0 then Writeln('positive'); else Writeln('non-positive'); fail to compile?
5. What value does case..of dispatch on when using a construct like case Score of 90..100: ...?
Was this page helpful?
You May Also Like
Pascal Best Practices
Practical conventions for writing clean, maintainable, and bug-resistant Pascal code, from naming and scoping to structured control flow.
Common Pascal Idioms
Recurring patterns experienced Pascal developers reach for: sentinel-controlled loops, set membership tests, and enumerated-type case dispatch.
Pascal Interview Questions
The core Pascal concepts interviewers probe most — parameter passing, memory management, and classic language 'gotchas.'
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics