Skip to main content

Pascal Script: Basics

TODO: Work in progress...

If you are not familiar with Pascal syntax, this page provides a quick guideintroduction to the basic structure of Pascala Scriptscript and theits control structures. Also,For checka outbroader introduction to the Pascal language, see this excellent tutorial by Tao Yue for the fundamentals of Pascal language..

Basic structure

ThisA script is thedivided basicinto structurean ofoptional declarations section and a script, which is split into declarations and themandatory executable statements.section:

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

The commented lines (starting withconst, //type), and var sections are just placeholders here,optional and havecan nobe impactomitted onif thenot executionneeded. ofThe thebeginend. script.

block

Statements

is

Therequired and is where all executable statements mustgo, beseparated withinby the begin and end keywords. All statements in the script use the semicolonsemicolons ;.

as terminator, except

In the finalexamples 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 usebelow, the following placeholders:placeholders are used:

  • <Condition> is evaluatedan expression that evaluates to be either true or false, for example X > 5.
  • <Action> is a code block that performs certainan operation, for example X := Y + 5.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 executing different parts of the code to execute depending on certaina conditions.condition.

If-then

if <Condition> then
begin
  <Action>
end;

If<Action> theis executed only if <Condition> is true, <Action>otherwise is executed. Otherwise, <Action>it 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.executed, Otherwise,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 tests a single expression (e.g. variable X) against multiple possible values (e.g. 1 and 2). Each value has several <Action> blocks, each with its own condition. Any given <Action> block. The values are checked in order of appearance, and the first matching block is executed only ifno itsfurther condition is met. The conditionsvalues are checked in the order of their appearance, until a matching condition is met.afterward. The else section is executed onlyexecutes if no other explicitly defined conditionmatch is met.found and is optional.

Loops

Loops allow executing a partblock of the code multipleto times,execute repeatedly until a certain condition is met.

For-to-do

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

The <Action> is executedexecutes a fixed number of times,times. as theThe iterator variable (I instarts thisat example) is continuously incremented by 1, starting from the initial value (X inand thisincrements example)by and1 on each iteration until theit targetreaches value (Y. To count in thisreverse, example)use downto instead of to.

While-do

while <Condition> do
begin
  <Action>
end;

Execute <Action> executes repeatedly as long as the condition<Condition> is true. The condition is checked before each iteration.iteration, Ifso theif conditionit is false atfrom the beginning,start, the loop doesbody notnever execute at all.executes.

Repeat-until

repeat
  <Action>
until <Condition>;

Execute <Action> asexecutes longrepeatedly until the condition<Condition> becomes falsetrue. The condition is checked after each iteration.iteration, Theso the loop always executes at least once.

Termination

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 main code block can be terminated before it completes using the Exit; statement,statement however,terminates thisthe willcurrent causescript animmediately, regardless of where it appears in the code:

if <Condition> then Exit;

Use it sparingly, because it causes abrupt terminationtermination, ofwhich thecan scriptmake scripts harder to read and should be used with caution.debug.

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