Usina BRAdvPL Guide
Sign in
← All topics
COMMANDUnder review

IF ... ELSEIF ... ELSE ... ENDIF

IF lCond1; ...; ELSEIF lCond2; ...; ELSE; ...; ENDIF

Chooses a path from sequential logical conditions.

IF ... ELSEIF ... ELSE ... ENDIFAdvPL
01 · OVERVIEW

Overview

The first true condition determines the executed block. Optional ELSE covers the remaining case and ENDIF closes the structure. Place specific conditions before general ones to avoid unreachable alternatives. The example classifies a fictional value without applying tax or business rules. (Sources: refs. 1–1.)

02 · SYNTAX

Syntax

IF lCond1; ...; ELSEIF lCond2; ...; ELSE; ...; ENDIF
03 · PRACTICAL EXAMPLE

Commented example

#Include "Protheus.ch"

User Function Ed483()
    Local nQty := 5
    Local cState := ""
    // Mutually exclusive conditions.
    If nQty < 0
        cState := "Negative"
    ElseIf nQty == 0
        cState := "Zero"
    Else
        cState := "Positive"
    EndIf
    ConOut(cState)
Return Nil
Expected result

Selects the positive alternative. Under review: editorial example neither compiled nor run in Protheus. Check includes, release, RDD and context before testing.

BEST PRACTICES
  • Avoid side effects in conditions and validate types before comparing.
  • Under review: editorial example neither compiled nor run in Protheus. Check includes, release, RDD and context before testing.

Related content

REFERENCES
  1. TOTVS. Estruturas de Decisão no AdvPL. TDN. Accessed on 2026-10-27.
Status
Under review
Page created on
Last reviewed on
Original language
Portuguese
Reviewed by
Usina.BR
0 approved comment(s)

Comments

There are no approved comments yet.

Sign in with Google or Microsoft to comment.

Powered by Usina Docs · Alpha