# Loops
DWScript supports the standard Pascal loops: `for`, `while`, and `repeat`.
## For Loop
The `for` loop is ideal when the number of iterations is known in advance.
```pascal
// Standard counting up
for var i := 1 to 5 do
PrintLn('Count: ' + i.ToString);
// Counting down
for var j := 10 downto 1 do
PrintLn('Countdown: ' + j.ToString);
// OUTPUT
// Count: 1
// Count: 2
// Count: 3
// Count: 4
// Count: 5
// Countdown: 10
// Countdown: 9
// Countdown: 8
// Countdown: 7
// Countdown: 6
// Countdown: 5
// Countdown: 4
// Countdown: 3
// Countdown: 2
// Countdown: 1
```
### Step Clause
DWScript extends the standard `for` loop with an optional `step` clause. Note that the **step value must always be positive**, even when counting down with `downto`.
```pascal
// Count by 2
for var i := 0 to 10 step 2 do
Print(i.ToString + ' ');
PrintLn('');
// Count down by 2 (Step is still positive)
for var j := 10 downto 0 step 2 do
Print(j.ToString + ' ');
// OUTPUT
// 0 2 4 6 8 10
// 10 8 6 4 2 0
```
## Variable Scope & Values
DWScript handles loop variables with predictable scoping and post-loop values.
### Inline Scope
When using `for var i := ...`, the variable `i` is scoped **only to the loop body**. It is not accessible after the loop.
### Post-Loop Values
When using a pre-declared variable, its value after the loop is **well-defined**. It will be the first value that failed the loop condition (unlike Delphi, where the value is considered undefined).
```pascal
var k: Integer;
for k := 1 to 3 do begin
// Loop body
end;
// k is now 4 (the value that broke the 'k <= 3' condition)
PrintLn('k after loop: ' + k.ToString);
// OUTPUT
// k after loop: 4
```
## While Loop
The `while` loop checks the condition *before* each iteration. It may run zero times if the condition is initially false.
```pascal
var n := 0;
while n < 3 do begin
PrintLn('Running: ' + n.ToString);
Inc(n);
end;
// OUTPUT
// Running: 0
// Running: 1
// Running: 2
```
## Repeat Loop
The `repeat` loop checks the condition *after* each iteration. It is guaranteed to run **at least once**.
```pascal
var i := 0;
repeat
PrintLn('Value: ' + i.ToString);
Inc(i);
until i > 2;
// OUTPUT
// Value: 0
// Value: 1
// Value: 2
```
## Loop Control
You can modify the execution flow within any loop using jump statements:
* **[break](/lang/control_flow/jumps)**: Terminate the loop immediately.
* **[continue](/lang/control_flow/jumps)**: Skip to the next iteration.
:::info
For iterating over collections like arrays, sets, or strings, use the more modern **[for..in iteration](/lang/control_flow/iteration)**.
:::
Loops
DWScript supports the standard Pascal loops: for, while, and repeat.
For Loop
The for loop is ideal when the number of iterations is known in advance.
// Standard counting upforvar i :=1to5do
PrintLn('Count: '+ i.ToString);// Counting downforvar j :=10downto1do
PrintLn('Countdown: '+ j.ToString);
DWScript extends the standard for loop with an optional step clause. Note that the step value must always be positive, even when counting down with downto.
// Count by 2forvar i :=0to10 step 2do
Print(i.ToString +' ');
PrintLn('');// Count down by 2 (Step is still positive)forvar j :=10downto0 step 2do
Print(j.ToString +' ');
Result
0 2 4 6 8 10
10 8 6 4 2 0
Variable Scope & Values
DWScript handles loop variables with predictable scoping and post-loop values.
Inline Scope
When using for var i := ..., the variable i is scoped only to the loop body. It is not accessible after the loop.
Post-Loop Values
When using a pre-declared variable, its value after the loop is well-defined. It will be the first value that failed the loop condition (unlike Delphi, where the value is considered undefined).
var k:Integer;for k :=1to3dobegin// Loop bodyend;// k is now 4 (the value that broke the 'k <= 3' condition)
PrintLn('k after loop: '+ k.ToString);
Result
k after loop: 4
While Loop
The while loop checks the condition before each iteration. It may run zero times if the condition is initially false.
var n :=0;while n <3dobegin
PrintLn('Running: '+ n.ToString);
Inc(n);end;
Result
Running: 0
Running: 1
Running: 2
Repeat Loop
The repeat loop checks the condition after each iteration. It is guaranteed to run at least once.
var i :=0;repeat
PrintLn('Value: '+ i.ToString);
Inc(i);until i >2;
Result
Value: 0
Value: 1
Value: 2
Loop Control
You can modify the execution flow within any loop using jump statements: