# Loop Structures

> Source: https://manual.pvxplus.com/pxplus-user-guide/programming-constructs/flow-control/loop-structures

Loop structures are intended for repetitive execution. They comprise a set of statements that are specified once but can execute multiple times based on defined counters or condition statements.

PxPlus includes the following directives for building controlled loops:

|  |  |  |
| --- | --- | --- |
|  | **FOR..NEXT** | Uses a counter to control the number of repetitions made. |
|  | **WHILE..WEND** | Tests a condition at the *beginning* the loop before starting. |
|  | **REPEAT..UNTIL** | Tests a condition at the *end* of the loop before repeating. |

A similar structure is used to open, query and read records from a data file. For information on this type of loop, see **[SELECT..FROM..NEXT RECORD](https://manual.pvxplus.com/pxplus-user-guide/file-handling/processing-data-files/file-processing-directives#select)**.

PxPlus also includes some directives that allow early termination of a currently active loop. See **[Flow Overrides](https://manual.pvxplus.com/pxplus-user-guide/programming-constructs/flow-control/flow-overrides)**.

## Infinite Loops

Sometimes, a loop is constructed so that it executes endlessly. The term ***infinite loop*** most often applies to a situation where this result is *not intended* and is likely due to a logic or system error. Carefully review your code to ensure that your loops are designed to halt normally.

For information on dealing with unexpected results during program operation, see **[Error Handling and Debugging](https://manual.pvxplus.com/pxplus-user-guide/development-tools/error-handling-and-debugging/overview)**.

## FOR..NEXT

The **[FOR](https://manual.pvxplus.com/directives/for)** directive is used to define the start of a *counter-controlled loop*.

The two types of **FOR..NEXT** loops, a ***Conventional Format*** and a ***Simplified Iteration Format***, are explained below.

***Conventional Format***

This **FOR..NEXT** syntax specifies a *control* variable (*var*), an *initial* value (*first*), an *ending* value (*last*), as well as an optional **STEP** value that can be used to set the increment/decrement to a specific value (default is 1):

```
FOR var = first TO last [ STEP val ]    ..NEXT [ var ]
```

The **[NEXT](https://manual.pvxplus.com/directives/next)** directive marks the end of the loop. The *control* variable used with the **NEXT** directive must match the variable in the corresponding **[FOR](https://manual.pvxplus.com/directives/for)** directive. The **NEXT** *control* variable is optional but should be used to maintain readability (especially for *nested* loops).

All statements following the **FOR** directive are executed in sequence until a **NEXT** directive is encountered. At this point, the *control* variable is incremented or decremented automatically. If the contents of the *control* variable exceeds the *ending* value (or falls below it, if decremented), control is transferred out of the **FOR..NEXT** loop, and execution resumes at the statement following the **NEXT** directive. Otherwise, control stays in the loop and is transferred back to the statement following the **FOR** directive.

***Example:***

```
0010 FOR I=1 TO 10
0020 PRINT I,
0030 NEXT I
RUN
1 2 3 4 5 6 7 8 9 10
```

Note:  
The conventional FOR..NEXT format does not test the condition until the NEXT statement is executed; therefore, the loop is always executed at least once, even if the control variable is initialized to a value exceeding the ending value.

***Simplified Iteration Format***

The following **FOR..NEXT** syntax is used to specify the number of iterations in the loop:

```
FOR var .. NEXT [ var ]
```

The simplified format executes the logic immediately following the **FOR** by the number of times specified in the numeric value *var*; therefore, FOR 1 will execute the loop once, and FOR 5 will execute the loop 5 times. A value of zero causes the loop to be skipped. An error is generated if the numeric value is not an integer or it is less than zero.

If *var* is a simple numeric variable, the system will first set *var* to 1, then increment up to its initial value. When *var* is 5, the loop will execute 5 times, with *var* starting at 1 and incrementing by 1 through each iteration to 5. At the completion of the loop,*var* will equal its initial value.

***Example:***

```
0010 LET X$="THIS IS A TEST"
0020 LET N=LEN(X$)
0030 FOR N
0040 IF X$(N,1)=" " THEN LET X$(N,1)="_"
0050 NEXT
0060 PRINT X$
RUN
THIS_IS_A_TEST
```

If the loop is exited prematurely (via **BREAK**, **POP** or **EXITTO**), the counter variable *var* will retain the value of the current iteration. Regardless of whether a simple numeric variable is used, **TCB(19)** will contain the current iteration count during the loop.

For syntax details, see **[FOR..NEXT](https://manual.pvxplus.com/directives/for)** directive.

## WHILE..WEND

Use the **[WHILE](https://manual.pvxplus.com/directives/while)** directive to specify a condition (numeric expression) at the beginning of a *condition-controlled loop*:

**WHILE** *expression* ..**WEND**

PxPlus executes all statements between **WHILE** and **WEND** repeatedly until the *expression* returns a ***false*** (0 *zero* ) result.

***Example:***

```
0010 INPUT "CAN WE TALK? <Y/N> ",R$
0020 WHILE UCS(R$)<>"N"
0030 INPUT "NAME PLEASE: ",N$
0040 INPUT "WHAT IS YOUR AGE "+N$+"? ",A$
0050 INPUT "CONTINUE? <Y/N> ",R$
0060 WEND
```

When PxPlus encounters a **WHILE** directive, it evaluates the *expression.* If the result is not 0 (*zero*), PxPlus continues execution until a corresponding **WEND** directive is encountered, at which point the *expression* is re-evaluated. PxPlus continues to loop back to the directive following the **WHILE** directive until a 0 value is reached. At this point, PxPlus advances to the next **WEND** directive, where it terminates the loop. Then control transfers to the statement following the **WEND**.

For syntax details, see **[WHILE..WEND](https://manual.pvxplus.com/directives/while)** directive.

## REPEAT..UNTIL

The **[REPEAT](https://manual.pvxplus.com/directives/repeat)** directive is used to begin a *condition-controlled loop.* In this structure, the **UNTIL** directive specifies the condition at the end of the loop:

**REPEAT ..UNTIL** *expression*

PxPlus executes all statements between **REPEAT** and **UNTIL** repeatedly until the *expression* returns a ***true*** (*non-zero*) result.

***Example:***

```
0010 LET X=1
0020 LET C=1
0030 REPEAT
0040 LET X=X*2
0050 LET C=C+1
0060 UNTIL X>100000
0070 PRINT C,X
RUN
18 131072
```

After the **REPEAT** directive, PxPlus executes all statements until the **UNTIL** directive; it then tests the condition specified. If the result is ***false*** (0 *zero*), PxPlus loops back to the directive following the **REPEAT** directive and resumes execution. If the result is ***true*** *(non-zero)*, the loop is terminated and execution continues from the statement following the **UNTIL** directive.

For syntax details, see **[REPEAT..UNTIL](https://manual.pvxplus.com/directives/repeat)** directive.
