Loops
Repeatedly execute a section of code in RapidScript.
Every type of loop contains these components:
- A condition expression: This expression is evaluated before every iteration. It must evaluate to a
bool
value. If it evaluates totrue
, the code block is executed and the loop continues to iterate. If it evaluates tofalse
, the code block is not executed and the loop terminates. - A block of code to execute every iteration.
Each type of loop adds some additional components.
For loops add the following components:
- The
for
keyword. - An initialization expression: This expression is executed before the first iteration of the for loop. It is typically used to set up and initialize necessary variables.
- An iteration expression: This expression is executed after every iteration. It is typically used to increment an iteration variable.
for (initialization; condition; iteration)
{
// code block to repeat
}
While loops add the following components:
- The
while
keyword.
while (condition)
{
// code block to repeat
}
Do While loops differ slightly from the other loops. Its code block is guaranteed to execute at least once, regardless of whether or not the condition evaluated to
true
. Subsequent iterations check the condition before executing and will terminate if it evaluates to false
. Do While loops add the following components:- The
do
keyword. - The
while
keyword.
do
{
// code block to repeat
} while (condition);
break;
Skip the remainder of the current iteration of a loop and terminate the enclosing loop.continue;
Skip the remainder of the current iteration of a loop (but continue looping).
Last modified 1yr ago