Usina BRAdvPL Guide
Sign in
← All topics
COMMANDPublished

FOR ... NEXT

FOR <variável> := <inicial> TO <final> [STEP <incremento>] ... NEXT

Repeats a block of commands a determined number of times using a variable as a counter.

IterationControl flowCommands
01 · OVERVIEW

Overview

FOR ... NEXT repeatedly executes the commands delimited by FOR and NEXT. The counter starts at the initial value, advances by the specified STEP — or by 1 when omitted — and stops after passing the final limit. A negative STEP iterates in descending order. EXIT terminates the loop and LOOP continues with the next iteration.

02 · SYNTAX

Syntax

FOR <variável> := <inicial> TO <final> [STEP <incremento>] ... NEXT

Parameters

variável
NuméricoRequired

Variable or array element used as the counter. Prefer declaring it explicitly as Local.

inicial
NuméricoRequired

Initial counter value, evaluated when the loop starts.

final
NuméricoRequired

Final counter limit, evaluated when the loop starts.

incremento
NuméricoOptional

Value added to the counter on each iteration. It may be negative and its expression is reevaluated each time.

Default: 1
03 · PRACTICAL EXAMPLE

Ascending iteration with STEP

User Function ExEvenNumbers()
    Local nNumber
    Local nSum := 0

    For nNumber := 0 To 100 Step 2
        nSum += nNumber
    Next

    MsgInfo("Sum of even numbers: " + CValToChar(nSum))
Return
Expected result

The counter starts at zero and advances by two. At the end, nSum contains the sum of even numbers from 0 through 100.

04 · PRACTICAL EXAMPLE

Descending iteration

User Function ExContagem()
    Local nContador

    For nContador := 5 To 1 Step -1
        ConOut("Contagem: " + CValToChar(nContador))
    Next
Return
Expected result

STEP -1 decreases the counter on each iteration, producing 5, 4, 3, 2 and 1.

05 · PRACTICAL EXAMPLE

Control with LOOP and EXIT

User Function ExControle()
    Local nItem

    For nItem := 1 To 20
        If nItem % 2 != 0
            Loop
        EndIf

        If nItem > 10
            Exit
        EndIf

        ConOut(CValToChar(nItem))
    Next
Return
Expected result

Odd values are skipped by LOOP. EXIT stops processing when the first even number greater than 10 is reached.

BEST PRACTICES
  • Declare the counter as Local before FOR to make its scope explicit.
  • Use a negative STEP when the initial value is greater than the final value.
  • Use EXIT when a condition allows the search to stop before its limit.
  • Use LOOP to skip the rest of the block and continue with the next iteration.
  • Keep the loop body short or extract extensive rules into helper functions.
COMMON PITFALLS
  • Changing variables used as initial or final limits inside the loop does not recalculate limits already evaluated at startup.
  • Directly changing the counter affects progression and may change the number of iterations.
  • A STEP incompatible with the expected direction may prevent the block from running or produce incorrect behavior.
  • If the counter does not exist, the documentation says it may be implicitly created as Private, making typos and scope dependencies harder to identify.

Related content

REFERENCES
  1. TOTVS. The FOR...NEXT Command. TDN. Created by Rafaela Rocha Hikiji; last changed by Julio Wittwer on September 4, 2018.
Status
Published
Page created on
Last reviewed on
Original language
Portuguese
Reviewed by
Usina.BR