Skip to main content

Pascal Script: Basics

TODO: Work in progress...

If you are not familiar with Pascal syntax, this page provides a quick introduction to the basic structure of a script and its control structures. For a broader introduction to the Pascal language, see this tutorial by Tao Yue.

Basic structure

A script is divided into an optional declarations section and a mandatory executable section:

const
  // Constant declarations
type
  // Type declarations
var
  // Variable declarations
begin
  // Executable statements
end.

The const, type, and var sections are optional and can be omitted if not needed. The beginend. block is required and is where all executable statements go, separated by semicolons ;.

In the examples below, the following placeholders are used:

  • <Condition> — an expression that evaluates to true or false, for example X > 5.
  • <Action> — a code block that performs an operation.

Comments

Comments can be written in two forms, single-line and multi-line:

// This is a single-line comment
{ This comment is a
  multi-line comment }

Branching

Branching allows different parts of the code to execute depending on a condition.

If-then

if <Condition> then
begin
  <Action>
end;

<Action> is executed only if <Condition> is true, otherwise it is skipped.

If-then-else

if <Condition> then
begin
  <Action-1>
end
else
begin
  <Action-2>
end;

If <Condition> is true, <Action-1> is executed, otherwise <Action-2> is executed.

Case/switch

case X of
  1:
    begin
      <Action-1>
    end;
  2:
    begin
      <Action-2>
    end;
  else
    begin
      <Action-3>
    end;
end;

This structure tests a single expression (e.g. variable X) against multiple possible values (e.g. 1 and 2). Each value has its own <Action> block. The values are checked in order of appearance, and the first matching block is executed — no further values are checked afterward. The else section executes if no match is found and is optional.

Loops

Loops allow a block of code to execute repeatedly until a condition is met.

For-to-do

for I := X to Y do
begin
  <Action>
end;

<Action> executes a fixed number of times. The iterator variable I starts at X and increments by 1 on each iteration until it reaches Y. To count in reverse, use downto instead of to.

While-do

while <Condition> do
begin
  <Action>
end;

<Action> executes repeatedly as long as <Condition> is true. The condition is checked before each iteration, so if it is false from the start, the loop body never executes.

Repeat-until

repeat
  <Action>
until <Condition>;

<Action> executes repeatedly until <Condition> becomes true. The condition is checked after each iteration, so the loop always executes at least once.

Loop control

Within any loop, two statements provide additional flow control:

  • Break; — immediately exits the loop and continues execution after it.
  • Continue; — skips the rest of the current iteration and moves to the next one.

Both are commonly used inside an ifthen block:

if <Condition> then Break;
if <Condition> then Continue;

Early exit

The Exit; statement terminates the current script immediately, regardless of where it appears in the code:

if <Condition> then Exit;

Use it sparingly, because it causes abrupt termination, which can make scripts harder to read and debug.