Usina BRAdvPL Guide
Sign in
← All topics
COMMANDPublished

DO CASE ... ENDCASE

DO CASE ... CASE <expressão lógica> ... [OTHERWISE ...] ENDCASE

Selects and executes the first block whose CASE expression evaluates to true.

DecisionControl flowCommands
01 · OVERVIEW

Overview

DO CASE ... ENDCASE organizes decisions with multiple alternatives. CASE expressions are evaluated in order, and only the block associated with the first true (.T.) condition runs. Later conditions are ignored. The optional OTHERWISE clause handles the case in which no CASE is true.

02 · SYNTAX

Syntax

DO CASE ... CASE <expressão lógica> ... [OTHERWISE ...] ENDCASE

Parameters

CASE expressão
LógicoRequired

Condition evaluated in sequence. The first .T. result selects the block to execute.

comandos
Instruções AdvPLRequired

AdvPL instructions belonging to the selected alternative.

OTHERWISE
CláusulaOptional

Optional block executed when every CASE condition returns .F.

03 · PRACTICAL EXAMPLE

Quarter classification

User Function ExPeriod()
    Local nMonth := Month(Date())
    Local cPeriod := ""

    Do Case
    Case nMonth <= 3
        cPeriod := "First quarter"
    Case nMonth <= 6
        cPeriod := "Second quarter"
    Case nMonth <= 9
        cPeriod := "Third quarter"
    Otherwise
        cPeriod := "Fourth quarter"
    EndCase

    MsgInfo(cPeriod)
Return
Expected result

Because months are tested in ascending order, the first matching limit determines the quarter. OTHERWISE covers the remaining months.

04 · PRACTICAL EXAMPLE

Specific conditions before general ones

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

    Do Case
    Case nTotal > 100000
        cFaixa := "Especial"
    Case nTotal > 50000
        cFaixa := "Alta"
    Case nTotal > 10000
        cFaixa := "Média"
    Otherwise
        cFaixa := "Padrão"
    EndCase
Return cFaixa
Expected result

The highest limit appears first. If the general condition came first, higher tiers would never be reached.

BEST PRACTICES
  • Order conditions from most specific to most general.
  • Use OTHERWISE to handle unexpected values or provide default behavior.
  • Keep each CASE short and focused on one responsibility.
  • Prefer DO CASE to several independent IF statements when only one alternative should run.
  • Use IF ... ELSEIF ... ELSE ... ENDIF when that form is clearer for the team.
COMMON PITFALLS
  • Only the first true CASE runs, even if later conditions would also be true.
  • A broad condition placed first may make later alternatives unreachable.
  • Without OTHERWISE, no internal instruction runs when all CASE expressions are false.
  • Every DO CASE must end with ENDCASE.
  • Conditions with side effects make decision order harder to understand and should be avoided.

Related content

REFERENCES
  1. TOTVS. The DO CASE...ENDCASE 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