Usina BRAdvPL Guide
Sign in
← All topics
COMMANDPublished

WHILE ... ENDDO

WHILE <expressão lógica> ... ENDDO

Repeats a block of commands while a logical expression remains true.

IterationControl flowCommands
01 · OVERVIEW

Overview

WHILE ... ENDDO evaluates its condition before each iteration. If it is true (.T.), the block runs; when it becomes false (.F.), processing continues after ENDDO. EXIT immediately terminates the loop, while LOOP returns to WHILE and reevaluates the condition without executing the remainder of the current block.

02 · SYNTAX

Syntax

WHILE <expressão lógica> ... ENDDO

Parameters

expressão lógica
LógicoRequired

Condition reevaluated before each iteration. The loop continues only while it returns .T.

comandos
Instruções AdvPLRequired

AdvPL instructions executed on each pass while the condition remains true.

03 · PRACTICAL EXAMPLE

Condition-controlled iteration

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

    While nNumber <= 100
        nSum += nNumber
        nNumber += 2
    EndDo

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

The condition is checked before each pass. nNumber advances by two until it exceeds 100.

04 · PRACTICAL EXAMPLE

Sequential table reading

User Function ExWhileTabela()
    DbSelectArea("SA1")
    SA1->(DbGoTop())

    While !SA1->(Eof())
        ConOut(SA1->A1_COD + " - " + SA1->A1_NOME)
        SA1->(DbSkip())
    EndDo
Return
Expected result

The loop processes each record until EOF() indicates the end of the work area. DbSkip() is essential for advancing.

05 · PRACTICAL EXAMPLE

LOOP and EXIT with safe updates

User Function ExWhileControle()
    Local nItem := 0

    While nItem < 20
        nItem++

        If nItem % 2 != 0
            Loop
        EndIf

        If nItem > 10
            Exit
        EndIf

        ConOut(CValToChar(nItem))
    EndDo
Return
Expected result

The counter is updated before LOOP. Odd values are skipped and EXIT terminates the loop after the desired limit.

BEST PRACTICES
  • Use WHILE when the number of iterations depends on a condition that changes during processing.
  • Ensure that some instruction in the block can make the condition false.
  • Use EXIT to stop early after finding the desired result.
  • Use LOOP to skip the rest of an iteration, but update condition-control variables first.
  • When reading tables, advance with DbSkip() and combine EOF() with any other required conditions.
COMMON PITFALLS
  • A condition that never becomes false creates an infinite loop.
  • LOOP may skip the counter update, DbSkip(), or another instruction required to terminate the loop.
  • Every WHILE requires a matching ENDDO; nested structures must be opened and closed correctly.
  • Conditions that access fields or functions must remain valid throughout all iterations.
  • Very long or deeply nested blocks are harder to test and maintain.

Related content

REFERENCES
  1. TOTVS. The WHILE...ENDDO Command. TDN. Created by Rafaela Rocha Hikiji; last changed on January 17, 2022.
  2. TOTVS. WHILE ... ENDDO. Framework TDN.
  3. TOTVS. Developing queries in Protheus. TDN.
Status
Published
Page created on
Last reviewed on
Original language
Portuguese
Reviewed by
Usina.BR