variableVariable or array element used as the counter. Prefer declaring it explicitly as Local.
AdvPL GuideFOR <variable> := <initial> TO <final> [STEP <increment>] ... 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 <variable> := <initial> TO <final> [STEP <increment>] ... NEXTvariableVariable or array element used as the counter. Prefer declaring it explicitly as Local.
initialInitial counter value, evaluated when the loop starts.
finalFinal counter limit, evaluated when the loop starts.
incrementValue 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 ExCountdown()
Local nCounter
For nCounter := 5 To 1 Step -1
ConOut("Countdown: " + CValToChar(nCounter))
Next
ReturnSTEP -1 decreases the counter on each iteration, producing 5, 4, 3, 2 and 1.
User Function ExLoopControl()
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. All rights reserved. TOTVS, Protheus, Microsiga and their respective logos are trademarks of their owners. AdvPL Guide is independent and uses limited, attributed excerpts justified by educational purposes. See the Terms of Use. Terms of Use
Comments
There are no approved comments yet.
Sign in with Google or Microsoft to comment.