$loop
$loop is the legacy name of $for: it takes the same arguments (a count, a list of values or a C-style header) and behaves the same way, but the block is closed with $endLoop instead of $endFor.
Syntax
$loop[count]
...
$endLoop
Parameters
| Parameter | Description | Required |
|---|---|---|
count |
Number of repetitions: a non-negative integer, otherwise the error “Expected a nonnegative integer loop count.” is raised. 0 runs the block zero times. |
Yes |
$loop[name;value1;value2;...] (one iteration per value) and $loop[i=0;i<5;i++] (C-style) are also accepted, exactly as for $for.
Opening & Closing
- Open:
$loop[count] - Close:
$endLoop
Every $loop must be closed with $endLoop; closing it with $endFor is an error (“Unexpected $endFor.”), as is a missing $endLoop (“Missing $endloop.”).
Loop variables
Inside the block, $i and $loopIndex give the zero-based index of the current repetition, and $loopCount the one-based count (see $for). $break leaves the loop and $continue jumps to the next repetition.
Examples
Simple Repetition
$loop[3]
Hello! This is repetition number $loopCount.
$endLoop
Output:
Hello! This is repetition number 1.
Hello! This is repetition number 2.
Hello! This is repetition number 3.
Stopping early
$loop[10]
$if[$i==3]
$break
$endif
Step $loopCount
$endLoop
Using with Variables
$var[counter;0]
$loop[5]
$var[counter;$sum[$var[counter];1]]
Count: $var[counter]
$endLoop
Output:
Count: 1
Count: 2
Count: 3
Count: 4
Count: 5
Notes
- For new code, prefer
$for/$endFor: it is the same loop with its own documentation. - Nested loops are supported;
$loopIndexand$loopCountrefer to the innermost one. - A loop is bounded by the engine’s execution limits: a very large count (or a loop that never ends) stops with the error “Execution step limit exceeded.”
Try it
Paste an example into the playground to preview its message (functions that act on Discord are not run), or build the same thing with blocks.