variávelVariable or array element used as the counter. Prefer declaring it explicitly as Local.
AdvPL GuideFOR <variável> := <inicial> TO <final> [STEP <incremento>] ... NEXTRepeats a block of commands a determined number of times using a variable as a counter.
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.
FOR <variável> := <inicial> TO <final> [STEP <incremento>] ... NEXTvariávelVariable or array element used as the counter. Prefer declaring it explicitly as Local.
inicialInitial counter value, evaluated when the loop starts.
finalFinal counter limit, evaluated when the loop starts.
incrementoValue added to the counter on each iteration. It may be negative and its expression is reevaluated each time.
Default:1User Function ExEvenNumbers()
Local nNumber
Local nSum := 0
For nNumber := 0 To 100 Step 2
nSum += nNumber
Next
MsgInfo("Sum of even numbers: " + CValToChar(nSum))
ReturnThe counter starts at zero and advances by two. At the end, nSum contains the sum of even numbers from 0 through 100.
User Function ExContagem()
Local nContador
For nContador := 5 To 1 Step -1
ConOut("Contagem: " + CValToChar(nContador))
Next
ReturnSTEP -1 decreases the counter on each iteration, producing 5, 4, 3, 2 and 1.
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
ReturnOdd values are skipped by LOOP. EXIT stops processing when the first even number greater than 10 is reached.
© 2026 Usina.BR. Todos os direitos reservados. TOTVS, Protheus, Microsiga e seus respectivos logotipos são marcas de titularidade da TOTVS S.A. AdvPL é uma linguagem de programação desenvolvida pela TOTVS. O AdvPL Guia é um projeto independente, sem vínculo, representação, patrocínio ou afiliação com a TOTVS S.A., suas franquias ou representantes.