Basic syntax and language elements
This section introduces the fundamental elements of the HelpNDoc scripting language. Understanding these basics will help you write and modify scripts to automate tasks in HelpNDoc, or create templates to control the documentation's output. This guide covers variables, operators, control structures, and comments, which form the building blocks of your scripts.
Variables
Declaration and Types
Variables are used to store data that your script can manipulate. In HelpNDoc's scripting language, you need to declare variables with a specific type before using them.
Common Data Types
See: Base types
- Integer: Stores whole numbers.
Example: var Age: Integer; - Float: Stores floating-point numbers (numbers with decimals).
Example: var Price: Float; - String: Stores sequences of characters (text).
Example: var Name: String; - Boolean: Stores true or false values.
Example: var IsComplete: Boolean; - TDateTime: Stores date and time values.
Example: var EventDate: TDateTime; - Array: Stores a collection of elements, all of the same type, in a fixed-size sequence. Arrays are zero-indexed, meaning the first element is accessed with index 0.
Example: var Numbers: array of Integer;
Example
var Age: Integer;
var Name: String;
var Price: Float;
var IsComplete: Boolean;
var EventDate: TDateTime;
var Scores: array[0..2] of Integer;
begin
Age := 25;
Name := 'Alice';
Price := 99.99;
IsComplete := True;
EventDate := Now();
Scores[0] := 85;
Scores[1] := 90;
Scores[2] := 95;
Print('Name: ' + Name);
Print('Age: ' + Age.ToString);
Print('Price: ' + Price.ToString);
Print('Is Complete: ' + IsComplete.ToString);
Print('Event Date: ' + DateTimeToStr(EventDate));
Print('Scores: ' + Scores[0].ToString + ', ' + Scores[1].ToString + ', ' + Scores[2].ToString);
end;
Operators
Operators are symbols that tell the script to perform specific mathematical, logical, or comparison operations.
Assignment Operator
- := (Assignment): Assigns a value to a variable. VariableName := Value;
Arithmetic Operators
- + (Addition): Adds two numbers. Total := 5 + 3;
- - (Subtraction): Subtracts one number from another. Difference := 10 - 2;
- * (Multiplication): Multiplies two numbers. Product := 4 * 3;
- / (Division): Divides one number by another. Quotient := 8 / 2;
Comparison Operators
- = (Equal to): Checks if two values are equal. IsEqual := (5 = 5);
- <> (Not equal to): Checks if two values are not equal. IsNotEqual := (5 <> 3);
- > (Greater than): Checks if the left value is greater than the right. IsGreater := (7 > 3);
- < (Less than): Checks if the left value is less than the right. IsLess := (2 < 4);
Logical Operators
- and: Returns true if both conditions are true. Result := (True and False);
- or: Returns true if at least one condition is true. Result := (True or False);
- not: Reverses the condition. Result := not(True);
Example
var
Sum, Difference: Integer;
var
IsValid: Boolean;
begin
Sum := 10 + 5;
Difference := 15 - 7;
IsValid := (Sum > Difference) and (Difference > 0);
end;
Control Structures
Control structures allow you to control the flow of your script based on conditions or repetitive tasks.
If Statements
Use if statements to execute code only if a specific condition is true.
Syntax:
if Condition then
begin
// Code to execute if the condition is true
end
else
begin
// Code to execute if the condition is false
end;
Example:
var
Age: Integer;
begin
Age := 25;
if Age >= 18 then
Print('You are an adult.')
else
Print('You are not an adult.');
end;
For Loops
Use for loops to repeat a block of code a specific number of times.
Syntax:
for Variable := StartValue to EndValue do
begin
// Code to repeat
end;
Example:
var
i: Integer;
begin
for i := 1 to 5 do
begin
Print('Iteration: ' + i.ToString);
end;
end;
While Loops
Use while loops to repeat a block of code as long as a condition is true.
Syntax:
while Condition do
begin
// Code to repeat
end;
Example:
var
Counter: Integer;
begin
Counter := 0;
while Counter < 5 do
begin
Print('Counter: ' + Counter.ToString);
Inc(Counter); // Increment the counter
end;
end;
Comments
Comments are used to add explanations or notes in your code. They are ignored by the script during execution.
Single-line Comments
Use // for single-line comments.
Example:
// This is a single-line comment
var
Age: Integer;
begin
Age := 30; // Setting the age
end;
Multi-line Comments
Use { } or (* *) for multi-line comments.
Example:
{
This is a multi-line comment
that spans multiple lines.
}
var
Name: String;
begin
Name := 'HelpNDoc User';
end;