Program Structure
A Pascal program has a clear structure consisting of a program declaration, a declaration section (const, var, type), and an execution section (begin...end.). Statements are separated by semicolons, and the final end must be followed by a period (.). Pascal's strict structure is designed to improve readability and maintainability, requiring a clear separation between variable declarations and executable statements.
Syntax
{ -----------------------------------------------
Basic structure of a Pascal program
----------------------------------------------- }
program ProgramName; { Declares the program name (optional but conventional) }
uses
LibraryName; { Include external units when needed }
const
{ Constant declarations (values that cannot change) }
CONSTANT_NAME = value;
type
{ Type definitions (used when defining custom types) }
TypeName = ExistingType;
var
{ Variable declarations (must appear before begin) }
varName : TypeName;
begin
{ Write executable statements here }
{ Each statement ends with a semicolon }
varName := value;
{ begin...end can be used as a compound statement }
begin
{ statements inside the block }
end; { <- block terminator uses a semicolon }
end. { <- the final end of the program uses a period }
{ -----------------------------------------------
Semicolon rules
----------------------------------------------- }
{ Semicolons separate statements (they are separators, not terminators) }
{ The statement immediately before end does not require a semicolon,
but adding one is also accepted }
var
x : integer;
y : integer;
begin
x := 10; { semicolon present }
y := 20; { semicolon present }
WriteLn(x + y) { semicolon optional before end }
end.
Syntax Reference
| Syntax | Description |
|---|---|
program Name; | Declares the name of the program. In Pascal, the program name conventionally matches the file name. |
uses LibraryName; | Loads external units (libraries). Common examples are SysUtils, Math, and Classes. |
const ConstName = value; | Declares a constant that cannot be changed at runtime. Written before the var section. |
type TypeName = definition; | Defines custom types (enumerated types, record types, array types, etc.). |
var varName : TypeName; | Declares a variable. Must be written before begin. Multiple variables can be declared together as var1, var2 : TypeName. |
begin | Marks the start of the execution section (or a compound statement). |
end. | Marks the end of the program body. The final end requires a period (.). |
end; | Marks the end of a block (if, for, begin...end, etc.). Uses a semicolon, not a period. |
varName := value; | Assigns a value to a variable. In Pascal, = is used for comparison and constant definitions; := is used for assignment. |
Sample Code
jjk_basic.pas
{ jjk_basic.pas — Sample demonstrating PROGRAM / BEGIN / END structure and semicolon rules }
{ Uses Jujutsu Kaisen character data to show }
{ how const/var declaration sections and execution sections are separated }
{
Compile and run:
fpc jjk_basic.pas && ./jjk_basic
}
program jjk_basic;
const
{ -----------------------------------------------
Constant declarations (cannot be changed at runtime)
----------------------------------------------- }
SCHOOL_NAME = 'Tokyo Metropolitan Jujutsu Technical High School';
MAX_GRADE = 3;
var
{ -----------------------------------------------
Variable declarations (all written before begin)
----------------------------------------------- }
sorcerer_name : string; { sorcerer's name }
grade : integer; { grade (1-4) }
cursed_energy : real; { cursed energy level }
is_special_grade: boolean; { special grade flag }
begin
{ -----------------------------------------------
Assignment (:= is the assignment operator)
----------------------------------------------- }
sorcerer_name := 'Itadori Yuji';
grade := 4; { grade 4 at enrollment }
cursed_energy := 3200.0;
is_special_grade := False;
{ -----------------------------------------------
Output to screen
Semicolons separate executable statements
----------------------------------------------- }
WriteLn('===== ', SCHOOL_NAME, ' =====');
WriteLn('Name : ', sorcerer_name);
WriteLn('Grade : ', grade);
WriteLn('Cursed energy: ', cursed_energy:8:1);
WriteLn('Special grade: ', is_special_grade);
WriteLn('Max grade : ', MAX_GRADE);
WriteLn('================================');
{ -----------------------------------------------
begin...end used as a compound statement
----------------------------------------------- }
if is_special_grade then begin
WriteLn('Recognized as a Special Grade Sorcerer.');
end else begin
WriteLn('Active as a Grade Sorcerer.');
end; { <- block terminator uses semicolon (not a period) }
WriteLn('"I want to become a person who can die in the right way."');
end. { <- the final end of the program uses a period }
fpc jjk_basic.pas && ./jjk_basic Free Pascal Compiler version ... Linking ./jjk_basic ===== Tokyo Metropolitan Jujutsu Technical High School ===== Name : Itadori Yuji Grade : 4 Cursed energy: 3200.0 Special grade: FALSE Max grade : 3 ================================ Active as a Grade Sorcerer. "I want to become a person who can die in the right way."
jjk_team.pas
{ jjk_team.pas — Sample managing data for multiple characters }
{ Uses three first-year Tokyo students from Jujutsu Kaisen to show }
{ program structure with constants, arrays, and a for loop }
{
Compile and run:
fpc jjk_team.pas && ./jjk_team
}
program jjk_team;
const
NUM_MEMBERS = 3;
var
names : array[1..NUM_MEMBERS] of string;
grades : array[1..NUM_MEMBERS] of integer;
cursed_energies: array[1..NUM_MEMBERS] of real;
i : integer;
total_energy : real;
begin
{ -----------------------------------------------
Data initialization
----------------------------------------------- }
names[1] := 'Itadori Yuji'; grades[1] := 4; cursed_energies[1] := 3200.0;
names[2] := 'Fushiguro Megumi'; grades[2] := 2; cursed_energies[2] := 2800.0;
names[3] := 'Kugisaki Nobara'; grades[3] := 3; cursed_energies[3] := 2100.0;
{ -----------------------------------------------
Display member list
----------------------------------------------- }
WriteLn('===== Tokyo First-Year Members =====');
total_energy := 0.0;
for i := 1 to NUM_MEMBERS do begin
WriteLn(i:2, '. ', names[i]);
WriteLn(' Grade: ', grades[i], ' Cursed energy: ', cursed_energies[i]:8:1);
total_energy := total_energy + cursed_energies[i];
end;
{ -----------------------------------------------
Display totals
----------------------------------------------- }
WriteLn('------------------------------------');
WriteLn('Team total cursed energy: ', total_energy:8:1);
WriteLn('Average cursed energy : ', (total_energy / NUM_MEMBERS):8:1);
WriteLn('=====================================');
end.
fpc jjk_team.pas && ./jjk_team
Free Pascal Compiler version ...
Linking ./jjk_team
===== Tokyo First-Year Members =====
1. Itadori Yuji
Grade: 4 Cursed energy: 3200.0
2. Fushiguro Megumi
Grade: 2 Cursed energy: 2800.0
3. Kugisaki Nobara
Grade: 3 Cursed energy: 2100.0
------------------------------------
Team total cursed energy: 8100.0
Average cursed energy : 2700.0
=====================================
Common Mistakes
Forgetting the period after end.
The final end that closes the program body requires a period (end.). The end that closes other blocks uses a semicolon (end;), but only the very last one in the file takes a period. Writing end; instead of end. at the program level causes a compile error.
Getting the order of var / const / type sections wrong
Pascal imposes ordering constraints on declaration sections. The general order is uses → const → type → var. Writing type after var causes a compile error in some implementations. The same ordering applies inside procedures and functions.
Trying to group multiple statements without begin
Control structures such as if, for, and while directly control only a single statement. To execute multiple statements together, they must be wrapped in a begin...end block. Omitting begin causes the second and subsequent statements to fall outside the control structure, leading to unintended behavior.
Overview
A Pascal program starts with program Name;, passes through the declaration section (const, var, type), and ends with the begin...end. execution section. Semicolons act as statement separators rather than terminators, so the statement immediately before end does not require one, though adding it is also accepted. The final end must be followed by a period (.), while end closing other blocks takes a semicolon. The assignment operator is :=; the equals sign = is used only for comparisons and constant definitions. Variables must be declared in the var section before begin; declarations inside the execution section are not allowed. These strict rules reflect Pascal's design as a language created for teaching programming. For details on creating and compiling source files, see Creating and running .pas files. For input and output, see WriteLn / Write / ReadLn / Read.
If you find any errors or copyright issues, please contact us.