Usina BRAdvPL Guide
Sign in
← All topics
COMMANDPublished

IF ... ENDIF

IF <expressão lógica> ... [ELSEIF <expressão>] ... [ELSE ...] ENDIF

Executes a block of commands according to the result of a logical expression.

DecisionControl flowCommands
01 · OVERVIEW

Overview

IF ... ENDIF controls instruction execution from a condition. When the expression is true (.T.), the block after IF runs. If it is false (.F.), ELSEIF clauses may test alternatives in order and ELSE may provide a default path. After one alternative runs, control continues after ENDIF.

02 · SYNTAX

Syntax

IF <expressão lógica> ... [ELSEIF <expressão>] ... [ELSE ...] ENDIF

Parameters

expressão lógica
LógicoRequired

Condition that determines whether the corresponding block runs.

ELSEIF
CláusulaOptional

Allows alternative conditions to be evaluated in sequence.

ELSE
CláusulaOptional

Optional block executed when no previous condition returns .T.

03 · PRACTICAL EXAMPLE

Simple decision with ELSE

User Function ExDueDate(dDueDate)
    Local cMessage := ""

    If Date() > dDueDate
        cMessage := "Due date exceeded"
    Else
        cMessage := "Within the deadline"
    EndIf

    MsgInfo(cMessage)
Return
Expected result

The comparison determines which block assigns the message shown to the user.

04 · PRACTICAL EXAMPLE

Multiple alternatives with ELSEIF

User Function ExFaixa(nTotal)
    Local cFaixa := ""

    If nTotal > 100000
        cFaixa := "Especial"
    ElseIf nTotal > 50000
        cFaixa := "Alta"
    ElseIf nTotal > 10000
        cFaixa := "Média"
    Else
        cFaixa := "Padrão"
    EndIf
Return cFaixa
Expected result

The first true condition sets the tier; remaining alternatives are ignored.

05 · PRACTICAL EXAMPLE

Combining logical operators

User Function ExAcesso(lAtivo, nNivel)
    Local lPermitido := .F.

    If lAtivo .And. nNivel >= 3
        lPermitido := .T.
    EndIf
Return lPermitido
Expected result

Access is allowed only when both conditions are true.

BEST PRACTICES
  • Make the condition explicitly produce a logical value.
  • Use ELSE when a default behavior is required.
  • Order ELSEIF clauses from most specific to most general.
  • Prefer short conditions and extract complex validations into clearly named functions.
  • Use DO CASE when many alternatives make ELSEIF sequences hard to read.
COMMON PITFALLS
  • Poorly grouped comparisons and logical operators may select an unexpected path.
  • Deeply nested IF statements reduce readability and increase the chance of closing the wrong structure.
  • A broad early ELSEIF condition may make later alternatives unreachable.
  • Although historical documentation states that NIL is treated as .F. since Protheus 5.07, relying on it hides errors; validate the condition type.
  • Every IF requires a matching ENDIF.

Related content

REFERENCES
  1. TOTVS. The IF...ENDIF Command. TDN. Created by Rafaela Rocha Hikiji; last changed on May 21, 2012.
  2. TOTVS. Decision Structures in AdvPL. TDN.
Status
Published
Page created on
Last reviewed on
Original language
Portuguese
Reviewed by
Usina.BR