Skip to main content

Pascal Script: Basics

TODO: Work in progress...

If you are not familiar with Pascal syntax, this page provides a quick guide to the basic structure of Pascal Script and the control structures. Also, check out this excellent tutorial by Tao Yue for the fundamentals of Pascal language.

Basic structure

This is the basic structure of a script, which is split into declarations and the executable statements.

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

The commented lines (starting with //) are just placeholders here, and have no impact on the execution of the script.

Statements

The executable statements must be within the begin and end keywords. All statements in the script use the semicolon ; as terminator, except the final end. which uses a dot as terminator.

This is where you program the logical flow and perform various operations, including calling the functions.

Let's take a look at the common control structures for defining the logical flow. For simplicity, we'll use the following placeholders:

  • <Condition> is evaluated to be either true or false, for example X > 5.
  • <Action> is a code block that performs certain operation, for example X := Y + 5.

Branching

Branching allows executing different parts of the code depending on certain conditions.

If-then

if <Condition> then 
begin 
  <Action>
end;

If the <Condition> is true, <Action> is executed. Otherwise, <Action> is skipped.

If-then-else

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

If the <Condition> is true, <Action-1> is executed. Otherwise, <Action-2> is executed. One of the actions is always executed.

Case/switch

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

This code structure has several <Action> blocks, each with its own condition. Any given <Action> block is executed only if its condition is met. The conditions are checked in the order of their appearance, until a matching condition is met. The else section is executed only if no other explicitly defined condition is met.

Loops

Loops allow executing a part of the code multiple times, until a certain condition is met.

For-to-do

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

The <Action> is executed a fixed number of times, as the iterator variable (I in this example) is continuously incremented by 1, starting from the initial value (X in this example) and until the target value (Y in this example).

While-do

while <Condition> do 
begin 
  <Action> 
end;

Execute <Action> as long as the condition is true. The condition is checked before each iteration. If the condition is false at the beginning, the loop does not execute at all.

Repeat-until

repeat 
  <Action> 
until <Condition>;

Execute <Action> as long until the condition becomes false. The condition is checked after each iteration. The loop executes at least once.

Termination

The main code block can be terminated before it completes using the Exit; statement, however, this will cause an abrupt termination of the script and should be used with caution.

Loops can be terminated early using the Break; statement, and can skip certain iterations using the Continue; statement.