|
| 1 | +# Range-based FOR statement (FB 6.0) |
| 2 | + |
| 3 | +## Description |
| 4 | + |
| 5 | +The range-based `FOR` statement is used to iterate over a range of numeric values. The iteration is performed in |
| 6 | +increasing order when used with `TO` clause and in decreasing order when used with `DOWNTO` clause. |
| 7 | + |
| 8 | +## Syntax |
| 9 | + |
| 10 | +``` |
| 11 | +[<label> :] |
| 12 | +FOR <variable> = <initial value> {TO | DOWNTO} <final value> [BY <by value>] DO |
| 13 | + <statement> |
| 14 | +``` |
| 15 | + |
| 16 | +## Notes |
| 17 | + |
| 18 | +- If omitted, `<by value>` is `1`. |
| 19 | +- `<variable>` also accepts parameters. |
| 20 | +- `<variable>`, `<initial value>`, `<final value>` and `<by value>` must be expressions of exact numeric types. |
| 21 | +- `BREAK [<label>]` can be used to exit the loop. |
| 22 | +- `CONTINUE [<label>]` can be used to restart the next loop iteration. |
| 23 | +- `<variable>` can be assigned by user code inside the loop. |
| 24 | + |
| 25 | +## Execution |
| 26 | + |
| 27 | +- `<initial value>` is evaluated and assigned to `<variable>`. If it is `NULL`, the loop is not executed. |
| 28 | +- `<final value>` is evaluated and assigned to a temporary variable. If it is `NULL`, the loop is not executed. |
| 29 | +- `<by value>` (or its default `1` value) is evaluated and assigned to a temporary variable. |
| 30 | + If it is `NULL`, the loop is not executed. If it is zero or negative, an error is raised. |
| 31 | +- Loop starts: |
| 32 | + - If it is not the first iteration: |
| 33 | + - If `<variable>` is `NULL`, the loop is exited. |
| 34 | + - `<variable>` is incremented (`TO`) or decremented (`DOWNTO`) by the cached `<by value>`. |
| 35 | + - `<variable>` is compared (less than or equal for `TO` or greater than or equal for `DOWNTO`) to the cached |
| 36 | + `<final value>` and if it is out of range, the loop is exited. |
| 37 | + - Loop continues to the next iteration. |
| 38 | + |
| 39 | +## Examples |
| 40 | + |
| 41 | +``` |
| 42 | +execute block returns (out integer) |
| 43 | +as |
| 44 | +begin |
| 45 | + for out = 1 to 3 do |
| 46 | + suspend; |
| 47 | +end |
| 48 | +
|
| 49 | +/* Result: |
| 50 | +1 |
| 51 | +2 |
| 52 | +3 |
| 53 | +*/ |
| 54 | +``` |
| 55 | + |
| 56 | +``` |
| 57 | +execute block returns (out integer) |
| 58 | +as |
| 59 | +begin |
| 60 | + for out = 9 downto 7 do |
| 61 | + suspend; |
| 62 | +end |
| 63 | +
|
| 64 | +/* Result: |
| 65 | +9 |
| 66 | +8 |
| 67 | +7 |
| 68 | +*/ |
| 69 | +``` |
| 70 | + |
| 71 | +``` |
| 72 | +execute block returns (out integer) |
| 73 | +as |
| 74 | +begin |
| 75 | + for out = 5 to 3 do |
| 76 | + suspend; |
| 77 | +end |
| 78 | +
|
| 79 | +/* Result: |
| 80 | +*/ |
| 81 | +``` |
| 82 | + |
| 83 | +``` |
| 84 | +execute block returns (out numeric(5,2)) |
| 85 | +as |
| 86 | +begin |
| 87 | + for out = 9 downto 7 by 0.5 do |
| 88 | + suspend; |
| 89 | +end |
| 90 | +
|
| 91 | +/* Result: |
| 92 | +9.00 |
| 93 | +8.50 |
| 94 | +8.00 |
| 95 | +7.50 |
| 96 | +7.00 |
| 97 | +*/ |
| 98 | +``` |
| 99 | + |
| 100 | +``` |
| 101 | +execute block |
| 102 | +as |
| 103 | + declare i integer; |
| 104 | +begin |
| 105 | + for i = 1 to 10 do |
| 106 | + begin |
| 107 | + insert into table1 values (i); |
| 108 | + insert into table2 values (i); |
| 109 | + end |
| 110 | +end |
| 111 | +
|
| 112 | +/* Result: |
| 113 | +10 records inserted into table1 |
| 114 | +10 records inserted into table2 |
| 115 | +*/ |
| 116 | +``` |
0 commit comments