Jump Statements

DWScript provides several reserved keywords to alter the flow of execution by jumping out of loops or subroutines. Unlike traditional Pascal where some of these might be system procedures, in DWScript they are first-class keywords.

exit

The exit keyword immediately terminates the execution of the current subroutine (procedure or function).

Basic Usage

In a procedure, exit simply returns control to the caller.

procedure Process(value : Integer);
begin
  if value < 0 then 
    exit; // Stop processing
    
  PrintLn(value);
end;

Returning Values

In a function, exit can be used to return a value immediately. DWScript supports several syntaxes for this, highlighting its status as a keyword:

  1. Standard Pascal: Assign to Result and call exit.
  2. Functional style: Pass the return value directly to exit.
  3. Keyword style: Use exit followed by the value (no parentheses).
function Calculate(x : Integer) : Integer;
begin
  // Method 1: Standard
  if x = 0 then begin
    Result := 0;
    exit;
  end;

  // Method 2: Parentheses (Delphi style)
  if x = 1 then
    exit(10);
    
  // Method 3: DWScript Keyword style
  if x = 2 then
    exit 20;

  Result := x * 10;
end;

PrintLn(Calculate(0));
PrintLn(Calculate(1));
PrintLn(Calculate(2));
PrintLn(Calculate(3));
Result
0
10
20
30

Global Scope

Using exit in the global scope (outside of any subroutine) immediately terminates the execution of the entire script.

var stopEarly := True;
PrintLn('Starting script...');

if stopEarly then
  exit;

PrintLn('This might not be reached.');
Result
Starting script...

break

The break keyword immediately terminates the innermost loop (for, while, or repeat) that contains it. Execution resumes at the statement following the loop.

var found := False;
for var i := 1 to 10 do begin
  if i = 5 then begin
    found := True;
    break; // Stop the loop
  end;
  PrintLn(i);
end;
Result
1
2
3
4

continue

The continue keyword skips the remainder of the current iteration of the innermost loop and proceeds to the next iteration.

  • In a for loop, the counter is incremented/decremented.
  • In a while or repeat loop, the condition is re-evaluated.
for var i := 1 to 5 do begin
  if i = 3 then 
    continue; // Skip 3
    
  PrintLn(i);
end;
Result
1
2
4
5
On this page