# For...Next
{: .no_toc }

Repeats a group of statements while the loop counter approaches its final value.

Syntax:

> **For** *counter* [ **As** *type* ] **=** *start* **To** *end* [ **Step** *step* ]  
> &nbsp;&nbsp;&nbsp;&nbsp; [ *statements* ]  
> &nbsp;&nbsp;&nbsp;&nbsp; [ **Continue For** \| **Exit For** ]  
> &nbsp;&nbsp;&nbsp;&nbsp; [ *statements* ] ...  
> **Next** [ *counter* ]

*counter*
: Numeric variable used as a loop counter. The variable can't be a Boolean or an array element.

*type*
: *optional* A numeric type used to declare *counter*.  
  When present it is equivalent to placing `Dim counter As type` immediately before the **For** statement.

*start*
: Initial value of *counter*.

*end*
: Final value of *counter*.

*step*
: *optional* Amount *counter* is changed each time through the loop. If not specified, *step* defaults to one.

*statements*
: *optional* One or more statements between **For** and **Next** that are executed the specified number of times.

**Continue For**
: *optional* Immediately skips remaining statements and begins next iteration, or exits the loop if no more iterations remain.  
  **Continue For** is often used after evaluating some condition, for example **If...Then**.

**Exit For**
: *optional* Immediately exits the body of the loop.  
  **Exit For** is often used after evaluating some condition, for example **If...Then**, and transfers control to the statement immediately following **Next**.

The *step* argument can be either positive or negative. The value of the *step* argument determines loop processing as follows:

| Value         | Loop executes if   |
| :------------ | :----------------- |
| Positive or 0 | *counter* <= *end* |
| Negative      | *counter* >= *end* |

After all statements in the loop have executed, *step* is added to *counter*. At this point, either the statements in the loop execute again (based on the same test that caused the loop to execute initially), or the loop is exited and execution continues with the statement following the **Next** statement.

> [!WARNING] BETA 1045
> With [LLVM](/LLVM/Getting-Started.md), a loop whose *step* is a `ByVal` **Long** or **Single** parameter of the procedure runs with a wrong step, or a wrong *start* or *end* when those are parameters too, when a later parameter of the procedure is declared `ByRef ... As Boolean` or `ByRef ... As Integer`. The wrong value depends on where the caller's argument for that parameter is stored: for a local variable, 20 in a 64-bit exe and 25 in a 32-bit exe, and other values in a run in the IDE. No error is raised. Declare that parameter `ByVal`, or place it before the *step* parameter.

> [!TIP]
> 
> Changing the value of *counter* while inside a loop can make code harder to read and debug.

**For...Next** loops can be nested by placing one **For...Next** loop within another. Give each loop a unique variable name as its *counter*. The following construction is correct:

```tb check_build
Dim I As Long, J As Long, K As Long
For I = 1 To 10 
  For J = 1 To 10 
    For K = 1 To 10 
    ' ... 
    Next K 
  Next J 
Next I 
```

> [!NOTE]
>
> When *counter* is omitted in a **Next** statement, execution continues as if *counter* is included. If a **Next** statement is encountered before its corresponding **For** statement, an error occurs.

### Example

This example uses the **For...Next** statement to create a string that contains 10 instances of the numbers 0 through 9, each string separated from the other by a single space. The outer loop uses a loop counter variable that is decremented each time through the loop.

```tb check_build
Dim Words, Chars, MyString 
For Words = 10 To 1 Step -1 ' Set up 10 repetitions. 
  For Chars = 0 To 9 ' Set up 10 repetitions. 
    MyString = MyString & Chars ' Append number to string. 
  Next Chars ' Increment counter 
  MyString = MyString & " " ' Append a space. 
Next Words 
```