Statements and Operators

Old Content - visit altium.com/documentation

Parent page: DelphiScript

DelphiScript Statements

A statement in DelphiScript is considered as simple when it does not contain any other statements. Examples of simple statements are assignment statements and procedure calls.

Simple statements:

 X := Y + 10; // assignment  ShowMessage(‘Hello World!’); // procedure call

A compound statement consists of multiple statements.

Compound statements:

 Begin   If A > B Then ShowMessage(‘A is bigger’);   Else ShowMessage(‘B is bigger’);   A := 0;   B := 0; End;

In Delphi script, when values are assigned to variables, the colon-equal operator is used; :=

When testing for equality, the equality operator is used; =

Conditional statements

DelphiScript has control statements that affect the flow of execution within a script. The most common control statement is the If..Then conditional statement.

If Then statement

The If..Then statement is used for conditional control. The syntax is:

 If Condition Then   Begin   // code here   End Else   Begin   // code here   End;

Case Of statement

To avoid a complex set of If statements, these can often be replaced with Case statements. A case statement in an expression is used to select a value, a list of possible values, or a range of values.

Any types can be used in a Case statement because DelphiScript is an untyped language. Case statements can have an Else statement that is executed if none of the labels correspond to the value of the selector (within the Case Of condition).

Example 1:

 Case Char of   '+' : Text := 'Plus sign';   '-' : Text := 'Minus sign';   '*', '/': Text := 'Multiplication or division';   '0'..'9': Text := 'Number';   a'..'z': Text := 'Lowercase character';   'A'..'Z': Text := 'Uppercase character'; else   Text := 'Unknown character'; End;

Example 2:

 Case UserName of   'Jack', 'Joe' : IsAdministrator := true;   ‘Fred' : IsAdministrator := false; else   Raise('Unknown User'); End;

With statement

The With statement is a DelphiScript shorthand. When referring to a record type variable (or an object), a with statement can be used instead of repeating its name each time.

Normal version:

 Form.Canvas.Pen.Width := 2; Form.Canvas.Pen.Color := clSilver;

Version using With:

 With Form.Canvas.Pen do Begin   Width := 2;   Color := clSilver; End;

For To Do loop

The For..To..Do statement provides a method for repeatedly looping through a block of code (one or more lines of code). The basic syntax is:

 For counter := start To end Do Begin   // code here End;

For loops are often used to initialize an array. The counter direction in a For..To..Do loop can be controlled by alternatively using the DownTo keyword to decrement the loop. DelphiScript provides the Break/Exit statement to exit a For..To..Do loop prematurely.

Repeat Until loop

The Repeat statement is executed repeatedly until the Boolean expression is true. The Repeat statement is always executed at least once.

Example:

 Repeat   Write('Enter a value (0..9): ');   ShowMessage(IntToStr(I)); Until (I >= 0) and (I <= 9);

While Do loop

A While statement is similar to a Repeat statement, except that the control condition is evaluated before the first execution of the statement sequence. Hence, if the condition is false, the statement sequence is never executed.

Example:

 Randomize; I := 0; While I < 1000 do Begin   I := I + Random (100);   Add ('Random Number: ' + IntToStr (I)); End;

Continue statement

The Continue statement jumps over the body of a loop, similar to the Goto statement. The continue statement causes the executing script to pass to the next iteration in the current For, While or Repeat loop.

Example:

 var   F: File;   i: Integer; Begin   For i := 0 to (FileListBox1.Items.Count - 1) do   Begin     Try     If FileListBox1.Selected[i] Then     Begin       If not FileExists(FileListBox1.Items.Strings[i]) then       Begin         MessageDlg('File: ' + FileListBox1.Items.Strings[i] + 'not found', mtError, [mbOk], 0);         Continue;       End;       AssignFile(F, FileListBox1.Items.Strings[i]);       Reset(F, 1);       ListBox1.Items.Add(IntToStr(FileSize(F)));       CloseFile(F);     End;     Finally       { do something here }     End;   End; End;

Goto Label statement

The Goto statement has the form Goto label which transfers the script execution to the statement marked by the specified label. To mark a statement, the label must be declared first and then the target statement preceded with the label and a colon: label: statement

A Label can be any valid identifier. The Label declaration and Goto statement must belong to the same code block within a script. Therefore it's not possible to jump into, or out of, a procedure or function.

Example:

 Label StartHere;   // code StartHere: Beep; Goto StartHere;

Exit statement

The Exit statement immediately returns from a function or procedure. If you call Exit from within a Try..Finally block, the Finally part gets executed before the subroutine returns. If the Exit procedure is called from within the main body of the script, then execution of the script will terminate.

Example:

 Begin   Server := SchServer;   If Server = Nil Then   Begin     ShowError('No SchServer started');     Exit; End;

Break Statement

The Break statement causes the executing script to exit out of the current For, While or Repeat loop. Script execution continues from the subsequent executable line after the current loop.

Example:

 Var   S: string; Begin   While True do   Begin     ReadLn(S);     Try       if S = '' then Break;       WriteLn(S);     Finally       { do something for all cases }     End;   End; End; 

DelphiScript Expression Operators

In general, an expression is a valid combination of constants, variables, literal values, operators and function results. Expressions are used to determine the value to assign to a variable, to compute the parameter of a function or to test for a condition. Expressions can include function calls.

DelphiScript has a number of logical, arithmetic, Boolean and relational operators. These operators are grouped by the order of precedence (see below), which is different to the precedence orders used by Basic, C etc. For example, the AND and OR operators have precedence compared to the relational one.

If you write a<b and c<d, DelphiScript will do the AND operation first, resulting in an error. To prevent this problem and define the priority, each < expression needs to be enclosed in parentheses: (a<b) and (c<d);

The supported DelphiScript operators listed below are shown by their order of precedence.

Operators grouped by Precedence

Note that Unary operators have the highest precedence.

Not

Boolean or bitwise NOT.

Multiplicative and Bitwise Operators

*

Arithmetic multiplication.

/

Floating point division.

div

Integer division.

mod

modulus (reminder of integer division).

and

Boolean or bitwise AND.

shl

Bitwise left shift.

shr

Bitwise right shift.

Additive Operators

+

Arithmetic addition, string concatenation.

-

Arithmetic subtraction.

or

Boolean or bitwise OR

xor

Boolean or bitwise EXCLUSIVE OR.

Relational and Comparison Operators (lowest precedence)

=

Test whether equal or not.

<>

Test whether not equal or not.

<

Test whether less than or not.

>

Test whether greater than or not.

<=

Test whether less than or equal to or not.

>=

Test whether greater than or equal to or not.

Also note that the ^ and @ operators are not supported by DelphiScript.

You are reporting an issue with the following selected text and/or image within the active document: