# loop statement

The loop statement repetitively executes
(loops) until an ending condition is met. The first set of statements,
if present, is executed at least once.

## Syntax

```
loop {statement.block1} [until|while] logical.exp{do} {statement.block2} repeat
loop
 {statement.block1}
[until|while] logical.exp{do}
 {statement.block2}
repeat
loop {statement.block} repeat
```

## Description

The loop...until and loop...while forms are repetitive loop functions
used to repeat a sequence of statements conditionally. The do word is optional.

If the until clause is used, looping continues as long as the expression following
it evaluates to false. If the while clause is used,
looping continues as long as the expression following it evaluates
to true.

The statements in the do clause
are executed as long as the loop is executed. If while or until is specified, the do is required.

The repeat statement defines
the end of the loop.

CAUTION: Loops with no breaks
in processing (such as looping waiting for input through system(14)), will place the system into a constant CPU
consumption state until the loop is exited, leading to poor system
performance.

If neither a while nor until clause is used, the loop never finishes. The use
of goto or exit is required
to exit the loop in this construct.

If the until expression is initially true or the while expression
is initially false, the statements specified in *statement.block1* are executed at least once and those specified in *statement.block2* are not executed.

## Example(s)

This loop terminates when the
calculated value of `x` is under 100. Each time through
the loop, `c` is incremented by one.

```
c = 1
loop
 x = c*2
 while x < 100 do
 c = c + 1
 repeat
```
This processes an active list. When the last item-ID is
read, the `exit` clause passes control to the next
executable statement after the repeat statement.
Notice the select list is the default active select list.

```
execute "select customers"
loop
 readnext item-ID else exit
 print item-ID
 repeat
```

## See also

- [Active list](https://d3codex.com/definitions/active-list/)
- [Boolean evaluation](https://d3codex.com/pickbasic-flashbasic/boolean-evaluation/)
- [Branching](https://d3codex.com/definitions/branching/)
- [continue statement](https://d3codex.com/pickbasic-flashbasic/continue-statement/)
- [do clause](https://d3codex.com/pickbasic-flashbasic/do-clause/)
- [exit statement](https://d3codex.com/pickbasic-flashbasic/exit-statement/)
- [for...next statement](https://d3codex.com/pickbasic-flashbasic/for-next-statement/)
- [readnext statement](https://d3codex.com/pickbasic-flashbasic/readnext-statement/)
- [Relational operators](https://d3codex.com/pickbasic-flashbasic/relational-operators/)
- [repeat statement](https://d3codex.com/pickbasic-flashbasic/repeat-statement/)
- [Statements and functions](https://d3codex.com/pickbasic-flashbasic/statements-and-functions/)
- [until clause](https://d3codex.com/pickbasic-flashbasic/until-clause/)
- [while clause](https://d3codex.com/pickbasic-flashbasic/while-clause/)

---
Source: https://d3codex.com/pickbasic-flashbasic/loop-statement/ - part of the D3Codex reference.
