Control the order of execution in your scripts using conditionals and loops. DWScript combines traditional Pascal readability with enhancements like for..in and loop step.
The if statement executes a block if a condition is true. The else part is optional.
var x := 10;
if x > 5 then
PrintLn('Greater than 5')
else
PrintLn('Less or equal');
// Nested
if x > 0 then begin
if x < 100 then
PrintLn('In range');
end; Greater than 5 In range
case is used for multi-way branching. In DWScript, it supports Integers, Characters, Enumerations, Strings, Booleans, Variants, and Floats.
var val := 7;
case val of
0: PrintLn('Zero'); // Single value
1..5, 7: PrintLn('Range and Seven'); // Mixed range and value
10, 20, 30: PrintLn('Specific list'); // List of values
else
PrintLn('Other'); // Default branch
end;
var fruit := 'Grape';
case fruit of
'Apple', 'Grape', 'Orange': PrintLn('First group');
'Peach', 'Plum', 'Watermelon': PrintLn('Second group');
else
PrintLn('Unknown fruit');
end;
// Case with Floats and Ranges
var f := 2.5;
case f of
0..0.99: PrintLn('Small');
1.0..2.99: PrintLn('Medium'); // Matches
else
PrintLn('Large');
end; Range and Seven First group Medium
Floating point ranges in case statements use inclusive boundaries (using standard <= and >= logic).
However, because floating-point math can have precision issues (e.g., 1.0 might be stored as 0.99999999999998), using case for exact boundaries can be risky. For mission-critical boundary logic, using if with an explicit epsilon or strict inequalities is recommended.
The with statement in DWScript differs from traditional Pascal. It is used as a scoping construct to create local aliases for expressions, improving readability and reducing repetition.
// Use 'with' to simplify local access to complex expressions
with fullName := WebRequest.QueryField['first_name'] + ' ' + WebRequest.QueryField['last_name'] do begin
if fullName.Trim <> '' then
PrintLn('Welcome, ' + fullName);
end;
// Declare multiple locals to use within a block
with a := 10, b := 20, c := 30 do begin
PrintLn('Sum: ' + (a + b + c).ToString);
end; Sum: 60
Important Difference from Delphi:
In traditional Delphi, the with statement implicitly introduces an object's members into the current scope (e.g., with myPoint do X := 10;).
In DWScript, the with statement does not introduce members into the current scope. Instead, it requires an explicit alias (e.g., with p := myPoint do p.X := 10;).
This design decision was made to avoid the "scope pollution" and ambiguity found in traditional Pascal, where it can be unclear whether a variable name refers to a local variable or a member of the object in the with block.
The variables declared in a with statement are only visible within its do block.