# 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.
```pascal
procedure Process(value : Integer);
begin
if value < 0 then
exit; // Stop processing
PrintLn(value);
end;
// OUTPUT NONE
```
### 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).
```pascal
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));
// OUTPUT
// 0
// 10
// 20
// 30
```
### Global Scope
Using `exit` in the global scope (outside of any subroutine) immediately terminates the execution of the entire script.
```pascal
var stopEarly := True;
PrintLn('Starting script...');
if stopEarly then
exit;
PrintLn('This might not be reached.');
// OUTPUT
// 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.
```pascal
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;
// OUTPUT
// 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.
```pascal
for var i := 1 to 5 do begin
if i = 3 then
continue; // Skip 3
PrintLn(i);
end;
// OUTPUT
// 1
// 2
// 4
// 5
```
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);beginif value <0thenexit;// 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:
Standard Pascal: Assign to Result and call exit.
Functional style: Pass the return value directly to exit.
Keyword style: Use exit followed by the value (no parentheses).
function Calculate(x :Integer):Integer;begin// Method 1: Standardif x =0thenbegin
Result :=0;exit;end;// Method 2: Parentheses (Delphi style)if x =1thenexit(10);// Method 3: DWScript Keyword styleif x =2thenexit20;
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 thenexit;
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;forvar i :=1to10dobeginif i =5thenbegin
found :=True;break;// Stop the loopend;
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.
forvar i :=1to5dobeginif i =3thencontinue;// Skip 3
PrintLn(i);end;